How can I create and populate my mock classes with Autofixture?

后端 未结 2 1175
梦如初夏
梦如初夏 2021-02-07 10:58

Currently I\'m using EF6 to implement my repositories inside a UnitOfWork. I also have created an In-Memory mock implementations (MockUnitOfWork & MockRepository) so that I

2条回答
  •  南笙
    南笙 (楼主)
    2021-02-07 11:32

    Yes, this is exactly what it's designed to do. See the example below. I'm using Mock instead of NSubstitute, because I'm not familiar with NSubstitute. You'll just have to pass another customization, and use NSubstitute syntax in setups.

    [SetUp]
    public  void SetUp()
    {
        // this will make AutoFixture create mocks automatically for all dependencies
        _fixture = new Fixture()
             .Customize(new AutoMoqCustomization()); 
    
        // whenever AutoFixture needs IUnitOfWork it will use the same  mock object
        // (something like a singleton scope in IOC container)
        _fixture.Freeze>(); 
    
        // suppose YourSystemUnderTest takes IUnitOfWork as dependency,
        // it'll get the one frozen the line above
        _sut = _fixture.Create(); 
    }
    
    [Test]
    public void SomeTest()
    {
        var id = _fixture.Create(); // some random id
        var fooObject = _fixture.Create(); // the object repository should return for id
    
        // setuping THE SAME mock object that wa passed to _sut in SetUp.
        // _fixture.Freeze>() returns the mock object, so whatever comes
        // next is Mock specific and you'll have to use NSubstitute syntax instead
        _fixture.Freeze>()
                .Setup(uow => uow.FooRepository.GetById(id))
                .Returns(fooObject); 
    
        // if this method will ask the unit of work for FooRepository.GetById(id)
        // it will get fooObject.
        var whatever = _sut.SomeMethod(id); 
    
        // do assertions
    }
    
    
    

    The beautiful thing about AutoFixture is that you don't have to create mocks for all of the dependencies of your system under test. If you're testing a functionality that uses only one dependency, you just Freeze it before creating the system under test. The rest of the dependencies will be mocked automatically.

    提交回复
    热议问题