如何在单元测试中使用 Moq 调用同一类中的另一个方法 [英] How to use Moq in unit test that calls another method in same class

查看:25
本文介绍了如何在单元测试中使用 Moq 调用同一类中的另一个方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Moq 框架的新手,对如何使用它有一些疑问.我会举一个例子,希望得到答案.

Hi I am new to Moq framework and have some questions about how to use it. I will give an example and hope for answers.

我有两个类,一个接口和一个实现:

I have two classes, an interface and and an implementation:

public class Vehicle{
   public string RegistrationNumber {get; set;}
   public long VehicleIdentifier { get; set; }
   public Tyre TyreSpecification { get; set; }
}

public class Tyre {
    public long NumberOfTyres {get; set;}
    public long TyreSize { get; set;}
}

public interface ISelecter {
   Vehicle GetVehicleByRegistrationNumber(string registrationNumber);
   Tyre GetTyreSpecification(long vehicleIdentifier);
}

public class Selecter : ISelecter
{
    public Vehicle GetVehicleByRegistrationNumber(string registrationNumber)
    {
        var vehicle = 'Database will give us the vehicle specification';

        //Then we do things with the vehicle object

        //Get the tyre specification
        vehicle.TyreSpecification = GetTyreSpecification(vehicle.VehicleIdentifier);

        return vehicle;

    }

    public Tyre GetTyreSpecification(long vehicleIdentifier)
    {
         var tyre = 'external manufacture system gets the tyre specification';

         //Then do thing with the tyre before returning the object


         return tyre;
    }
}

我想为这些方法编写两个测试.问题是当我为 GetVehicleByRegistrationNumber 编写测试时,我不知道如何模拟对 GetTyreSpecification 的方法调用.

I want to write two tests for those methods. The problem is when I write the test for GetVehicleByRegistrationNumber I do not know how to mock the method call to GetTyreSpecification.

测试方法如下所示:

[TestClass]
public class SelecterTest
{
    [TestMethod]
    public void GetTyreSpecification_test()
    {
        //Arrange
        var tyre = new Tyre { NumberOfTyres = 4, TyreSize = 18 };

        var mockSelecter = new Mock<ISelecter>();
        mockSelecter.SetUp(s=>s.GetTyreSpecification(It.IsAny<long>())).Returns(tyre);

        //Act
        var tyreSpec = mockSelecter.Object.GetTyreSpecification(123456);

        //Assert
        Assert.IsTrue(tyreSpec.NumberOfTyres == 4 && tyreSpec.TyreSize == 18);
    }

    [TestMethod]
    public void GetVehicleByRegistrationNumber_test()
    {
        //Arrange
        var vehicle= new Vehicle { VehicleIdentifier = 123456, RegistrationNumber = ABC123, TyreSpecification = new Tyre { Tyresize = 18, NumberOfTyres = 4 }};

        var mockSelecter = new Mock<ISelecter>();
        mockSelecter.SetUp(s=>s.GetVehicleByRegistrationNumber(It.IsAny<string>     ())).Returns(vehicle);

        //Act
        var vehicle = mockSelecter.Object.GetVehicleByregistrationNumber(123456);

        //Assert
        Assert.IsTrue(vehicle.Registrationnumber == "ABC123";
    }
}

在测试方法 GetVehicleByRegistrationNumber_test 中,我如何模拟对 getTyreSpecification 的调用?

In the test method GetVehicleByRegistrationNumber_test how do I mock the call to getTyreSpecification?

推荐答案

您不应该尝试在您尝试测试的类上模拟方法.模拟框架用于用假调用替换对您的类所接受的依赖项的实际调用,以便您可以专注于测试您的类的行为,而不会被它具有的外部依赖项分心.

You shouldn't be trying to mock a method on the class you're trying to test. Mocking frameworks are used to replace the actual calls made to dependencies that your class takes in with fake calls so that you can focus on testing the behaviour of your class without being distracted by external dependencies that it has.

您的 Selecter 类没有引入任何外部依赖项,因此您无需模拟任何内容.如果您不需要并测试实际代码本身,我将始终主张不要嘲笑.显然,为了保持测试的原子性,您需要模拟对外部依赖项的调用(如果有).

There are no external dependencies taken in by your Selecter class so you don't need to mock anything. I would always advocate not mocking if you don't have to and testing the actual code itself. Obviously, to keep your test atomic, you would need to mock calls to external dependencies if there were any.

这篇关于如何在单元测试中使用 Moq 调用同一类中的另一个方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆