Replacing an existing registration after you have already worked with the container is unlikely to ever have the behavior you expect (and this is true for all DI libraries) and that the reason the Simple Injector container is locked. This is described in more detail here (as @qujck already pointed out).
First of all, if you are writing unit tests, you should not use the container at all. Your unit tests must create the class itself or you extract this logic for a convenient factory method, for example:
private static MailNotifier CreateMailNotifier( IDeposit deposit = null, ISendMail mailSender = null, ILog logger = null) { return new MailNotifier( deposit ?? Substitute.For<IDeposit>(), mailSender ?? Substitute.For<ISendMail>(), logger ?? Substitute.For<ILog>()); }
This factory method is a variant of the Test Data Builder template.
Using optional parameters, it allows the unit test to specify only the fake implementation that is required during testing:
public void Notify_WithValidUser_LogsAMessage() {
If you use the container, because creating a class under control manually is too cumbersome, this indicates a problem in the tested class (most likely, this is a violation of the principle of uniform responsibility). Prevent the use of tools to solve problems in your design; your code is talking to you.
However, for integration tests it is much more convenient to use a container, but in this case you just need to create a new container for each integration test. This way you can add or replace IWebServiceOrder without any problems.
source share