如何在单元测试中模拟功能

时间:2018-07-03 09:59:09

标签: c# unit-testing nmock

我如何通过NMock3对GetData进行单元测试?

如果可以模拟prcessA.Run和ProcessA内部的“结果”,那就太好了。

IAnotherService不能作为GetData的参数,因为它取决于GetData中的已处理值。

有什么想法吗?

服务1

public class Service1 : IService1
{
    public Service1()
    {
    }

    public string GetData(int a)
    {
        // some process depends on input
        int b = a * new Random().Next();

        IAnotherService anotherService = new AnotherService(b);
        ProcessA processA = new ProcessA(anotherService);
        processA.Run();
        return processA.result;
    }
}

简化的流程A

public class ProcessA
{
    public string result;
    private IAnotherService anotherService;

    public ProcessA(IAnotherService anotherService)
    {
        this.anotherService = anotherService;
    }

    public void Run()
    {
        // Some other process here
        this.result = this.anotherService.Run();
    }
}

TestMethod1

[TestMethod]
public void TestMethod1()
{
    using (mockFactory.Ordered())
    {
        // Nothing to Mock
    }

    IService1 service1 = new Service1();
    string aaa = service1.GetData(1);

    Assert.AreEqual("XXX", aaa);
}

1 个答案:

答案 0 :(得分:2)

如前所述,您需要模拟依赖服务并设置您期望返回的内容。

我在下面做了一个测试,它可以工作。我使用起订量,但原理是一样的。

 public interface IAnotherService
 {
     string Run();
 }

public class ProcessA
{
    public string result;
    private readonly IAnotherService _anotherService;

    public ProcessA(IAnotherService anotherService)
    {
        this._anotherService = anotherService;
    }

    public string Run()
    {
        // Some other process here
        return _anotherService.Run();
    }   
}

然后运行测试

[Test]
public void TestMethod1()
{
   //Create a Mock
   var mockService = new Mock<IAnotherService>();
   //Set the expected result
   mockService.Setup(method => method.Run()).Returns("XXX");

   //Inject the mock
   var process = new ProcessA(mockService.Object);
   var result = process.Run();

   //Assert the result     
   Assert.AreEqual("XXX", result);
}

编辑

如前所述,我已对答案进行了编辑,以期使您满怀希望。

 public interface IService1
    {
        string GetData(int a);
        int ValueForB { get; set; }
    }

public class Service1Consumer : IService1
{
    private readonly IAnotherService _anotherServiceImplementation;

    public Service1Consumer(IAnotherService service)
    {
        _anotherServiceImplementation = service;
    }

    public string GetData(int a)
    {
        ValueForB = a * new Random().Next();
        _anotherServiceImplementation.ValueFor = b;
        var processA = new ProcessA(_anotherServiceImplementation);
        return processA.Run();
    }
}

public interface IAnotherService
{
    int ValueForB { get; set; }
}

public class AnotherService : IAnotherService
{
}

public class ProcessA
{
    public string result;
    private readonly IAnotherService _anotherService;

    public ProcessA(IAnotherService anotherService)
    {
        _anotherService = anotherService;
    }

    public string Run()
    {
        return "XXXX";
    }
}

然后进行测试。

  [Test]
  public void TestMethod1()
  {
      //Create a Mock
      var mockAnotherService = new Mock<IAnotherService>();
      //Set the property value when called.
      mockAnotherService.Setup(method => method.ValueForB).Returns(10);//Test 1

      var service1Consumer = new Service1Consumer(mockAnotherService.Object);
      var result = service1Consumer.GetData();

      Assert.AreEqual("XXXX",result);

   }

希望能为您指明正确的方向。 谢谢