简体   繁体   English

如果IRepository调用没有返回值,则使用Moq解析测试依赖项的目的

[英]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. 我是TDD的新手,我正在使用一堆XUnit,AutoFixture和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. 我想知道moq的用途是什么,当你测试一个函数调用一个命中数据库的Repository时。

For example: 例如:
I have a simple project structure where my BLL references my DAL. 我有一个简单的项目结构,我的BLL引用了我的DAL。 In my normal application an IRepository is injected into the ProductBLL.cs. 在我的正常应用程序中,IRepository被注入到ProductBLL.cs中。 So I see that Moq helps me inject the same repository in my test. 所以我看到Moq帮助我在测试中注入了相同的存储库。 (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? 如果使用Moq并且没有返回数据,是否可以测试这样的函数? In the examples below sut.GetProducts() returns nothing and in my actual code it hits the database and does return something. 在下面的示例中,sut.GetProducts()不返回任何内容,在我的实际代码中它会命中数据库并返回一些东西。

Lets say I have a simple test that does this with Moq: 让我说我有一个简单的测试,用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) 这是一个使用AutoFixture结合Moq的简单测试:(我想在这个例子中,AutoFixture至少会返回3行虚拟数据)

    [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. 如果GetProducts只是调用存储库GetProducts对结果做任何事情,那么如果它值得测试它可能是有争议的。 However, in general GetProducts might do many other things, for example: filtering, grouping, changing format and many other things. 但是,通常GetProducts可能会执行许多其他操作,例如:过滤,分组,更改格式和许多其他内容。

In this case you may validate much more than only checking if GetProducts returns some results. 在这种情况下,您可以验证的不仅仅是检查GetProducts返回某些结果。 For example, let's assume that the repository returns objects of type A but GetProducts returns objects of type B . 例如,假设存储库返回类型为A对象,但GetProducts返回类型为B对象。 It means that data must be copied from objects of type A to objects of type B . 这意味着必须将数据从类型A的对象复制到类型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. 其次, GetProducts可以使用存储库的1,2或更多方法。 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. 这个这个问题是关于验证Moq中方法调用的正确顺序。 This question is about verifying a number of calls in Moq. 这个问题是关于验证Moq的一些电话。 I also strongly suggest to read this tutorial. 我也强烈建议您阅读教程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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