Overriding a service in bUnit

64 views Asked by At

I have a TestContext Setup() method that adds all the services my pages need. Very nice having one place that sets up all 30+ services.

Sometimes I need a different mock service for a test. So I need to replace the default service.

I've tried setting the specific one after I call my Setup() and that works. Is this supported? That the last Services.AddSingleton() is used? Or is this just how the code is written at present and it could change?

I tried calling Services.Remove<IMyService>() but that gives me a compiler error.

1

There are 1 answers

1
Egil Hansen On BEST ANSWER

Just register the service in your test. The latest registered services is returned, unless a component gets an IEnumerable<IMyService> injected, then all registered IMyServices gets injected.