使用 MOQ 设置和退货的正确方法

时间:2023-02-26
本文介绍了使用 MOQ 设置和退货的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我是 MOQ 新手,我对设置方法有点困惑.下面的示例显示了我需要测试的一种方法.被测方法返回两个日期的最新时间,因此我创建了两个 datetime 对象并将它们传递给我的函数.我感到困惑的部分是返回调用.这忽略了我的方法中的逻辑并返回我告诉它的内容.例如,如果我说返回(date2),那么无论逻辑如何,断言都会通过.我做错了吗?

Im new to MOQ and I am a little confused with the setup method. The example below shows one method that i need to test. The method under test returns the latest time from two dates, so I create two datetime objects and pass them to my function. The part where I'm confused is the returns call. This ignores the logic in my method and returns what I tell it to. For example if i say returns(date2) then the assert passes regardless of the logic. Am I doing something wrong?

public virtual DateTime LatestTime(DateTime t1, DateTime t2)
{
   if (t1.CompareTo(t2) > 0)
      return t1;

    return t2;
}

[Test]
[Category("Catalogue service")]
public void TestLatestTimeReturnsCorrectResult()
{
    //Arrange
    DateTime date1 = new DateTime(2014, 07, 25, 13, 30, 01);
    DateTime date2 = new DateTime(2014, 07, 25, 13, 30, 00);

    MockCatalogueService.Setup(x => x.LatestTime(date1, date2)).Returns(date2);

    //Act
    DateTime retDate = MockCatalogueService.Object.LatestTime(date1, date2);

    //Assert
    Assert.That(retDate == date2);
}

推荐答案

你以错误的方式使用 Moq.它旨在替代您测试的类所依赖的一些实现.例如,您正在测试一些使用数据库存储库的类:

You use Moq in the wrong way. It is intended to substitute some implementations your tested class is dependent on. For example, you are testing some class that uses a DB repository:

public class MyService
{
    private IMyDbRepository _repos;
    
    public MyService(IMyDbRepository dbRepos)
    {
        _repos = dbRepos;
    }

    public string[] GetClientNames()
    {
        return _repos.GetAllClients().Where(c=>!c.IsDisabled).OrderBy(c=>c.Name).ToArray();
    }
}

您需要测试 GetClientNames() 方法.但是你不能直到你有 IMyDbRepository 实例.仅仅为了测试排序和过滤客户端的方法而创建和填充数据库太复杂和错误.

You need to test the GetClientNames() method. But you can't until you have IMyDbRepository instance. It's too complicated and wrong to create and fill database just to test method of sorting and filtering clients.

出路是使用Moq:

[Test]
public void TestGetAllClientsDoesNotReturnDisabledUsers()
{
    var dbReposMock = new Mock<IMyDbRepository>();
    dbReposMock.Setup(r=>r.GetAllClients()).Returns(
                      new []{ new Client { Name="AAA", IsDisabled=true },
                              new Client { Name="BBB", IsDisabled=false } });

    var myTestingService = new MyService(dbReposMock.Object);//You pass here the autogenerated object which follows the described primitive behavior without requiring DB at all.
    var clientNames = myTestingService.GetClientNames();
    Assert.AreEqual(1, clientNames.Length);
    Assert.AreEqual("BBB", clientNames[0]);
}

所以 Moq 允许您动态(在运行时)生成假类(非密封)或接口实现,并使用它们将您的测试功能与其他一切分离.因此,如果数据库结构中出现错误,您只会看到少数数据库测试失败,并且可以很容易地识别出问题所在,而如果您没有将代码与最小起订量解耦,则所有层的 100 个不同测试都失败了.

So Moq allows you to generate fake class (non-sealed) or interface implementations on the fly (in runtime) and use them to decouple your testing functionality from everything else. Consequently, if a bug appears in the DB structure, you see only a few DB-tests failing and can easily identify what is the problem comparing to the case when 100 different tests from all layers failing if you didn't decouple the code with Moq.

这篇关于使用 MOQ 设置和退货的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:NUnit 模拟不适用于单例方法 下一篇:使用 Moq 模拟对 MySQL 数据库的插入查询

相关文章

最新文章