简体   繁体   中英

The purpose of using Moq to resolve test dependencies if IRepository calls return no values

I am new to TDD and I am using a stack of XUnit, AutoFixture, and Moq. I am wondering what the purpose of moq is when your testing a function that makes a call to a Repository which hits the database.

For example:
I have a simple project structure where my BLL references my DAL. In my normal application an IRepository is injected into the ProductBLL.cs. So I see that Moq helps me inject the same repository in my test. (But what is the point if the functions that hit the database don't work?)

My question:
Lets say I am making tests to test my business layer and I want to test functions that call my repository which calls my database in my actual code. If using Moq and no data is returned is it possible to test functions like this? In the examples below sut.GetProducts() returns nothing and in my actual code it hits the database and does return something.

Lets say I have a simple test that does this with Moq:

    [Fact]
    public void TestGetProducts_Manual_Moq()
    {
        // Arrange
        var mockCustomerRepository = new Mock<IProductRepository>();

        var sut = new ProductBLL(mockProductRepository.Object);

        // Act
        var result = sut.GetProducts();

        // Assert
        Assert.True(result.Count > 0);
    }

Here is a simple test using AutoFixture combined with Moq to the fullest: (I would think in this example AutoFixture would at least return 3 rows of dummy data)

    [Fact]
    public void TestGetProducts_AutoMoq()
    {
        // Arrange
        Fixture fixture = new Fixture();

        // Add auto mocking support for Moq
        fixture.Customize(new AutoMoqCustomization());

        var sut = fixture.Create<ProductBLL>();

        // Act
        var result = sut.GetProducts();

        // Assert
        Assert.True(result.Count > 0);
    }

If GetProducts simply calls a repository and do nothing with results it might be debatable if it is worth testing it. However, in general GetProducts might do many other things, for example: filtering, grouping, changing format and many other things.

In this case you may validate much more than only checking if GetProducts returns some results. For example, let's assume that the repository returns objects of type A but GetProducts returns objects of type B . It means that data must be copied from objects of type A to objects of type B . On this occasion some data can be removed, some reformatted etc. Your tests should verify if the final result is correct.

Secondly, GetProducts may use 1, 2 or more methods of the repository. Some of these methods may be used only once and some many times. If so, you may use a mock to verify if methods were used:

  • a correct amount of times
  • in the correct order
  • with correct values of parameters

This and this questions are about verifying a correct order of methods calls in Moq. This question is about verifying a number of calls in Moq. I also strongly suggest to read this tutorial.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM