Override Autofac registration - Integration tests with DI

后端 未结 2 896
耶瑟儿~
耶瑟儿~ 2021-01-13 12:18

I write integration tests for my application, and use my container for this. I want to be able to register all the components as I do in real running, and then override some

2条回答
  •  忘掉有多难
    2021-01-13 12:42

    Autofac will use the last registered component as the default provider of that service

    From the AutoFac documation.

    In your arrange/setup/testInit phase register the mocks, then resolve the SUT:

    [SetUp]
    public void TestInit()
    {
        Mock mock = new Mock();
        builder.RegisterInstance(mock.object).As();
        ...
        ...
        _target = builder.Resolve();
    }
    

    Note:

    Singletons, static members and SingletonLifestyle(registration) may cause some troubles....

提交回复
热议问题