C# 多接口单元测试
我有一个Web API项目,其中包含从API控制器调用的以下服务类。我想使用Moq框架为下面的类编写单元测试用例。如何使用Moq构造多个接口?如果不能使用Moq,是否有其他框架C# 多接口单元测试,c#,unit-testing,moq,vs-unit-testing-framework,C#,Unit Testing,Moq,Vs Unit Testing Framework,我有一个Web API项目,其中包含从API控制器调用的以下服务类。我想使用Moq框架为下面的类编写单元测试用例。如何使用Moq构造多个接口?如果不能使用Moq,是否有其他框架 public class MyService : IMyService { private readonly IInterface1 _interface1; private readonly IInterfaces2 _interface2; private readonly IInterfac
public class MyService : IMyService
{
private readonly IInterface1 _interface1;
private readonly IInterfaces2 _interface2;
private readonly IInterface3 _interface3;
public MyService(IInterface1 interface1,IInterface2 interface2,IInterface3 interface3)
{
_interface1=interface1;
_interface2=interface2;
_interface3=interface3;
}
public SomeModel MyMethod1(1Model model)
{
//do something here....
}
public SomeMode2 MyMethod2(Model2 model)
{
//do something here....
}
public SomeMode3 MyMethod3(Model3 model)
{
//do something here....
}
}
您可以使用来解决依赖项注入问题
var mocker = new AutoMoqer();
var myService = mocker.Create<MyService>();
var interface1 = mocker.GetMock<IInterface1>();
var mocker=new AutoMoqer();
var myService=mocker.Create();
var interface1=mocker.GetMock();
假设您有以下接口:
public interface IOne
{
int Foo();
}
public interface ITwo
{
int Foo(string str);
}
public class Some
{
private readonly IOne one;
private readonly ITwo two;
public Some(IOne one, ITwo two)
{
this.one = one;
this.two = two;
}
public void Work()
{
// Uses one and two
}
}
您有一个依赖于上述接口的类:
public interface IOne
{
int Foo();
}
public interface ITwo
{
int Foo(string str);
}
public class Some
{
private readonly IOne one;
private readonly ITwo two;
public Some(IOne one, ITwo two)
{
this.one = one;
this.two = two;
}
public void Work()
{
// Uses one and two
}
}
现在,您想测试Work()
方法,并想模拟依赖项,下面是如何:
// Arrange
// Let's set up a mock for IOne so when Foo is called, it will return 5
var iOneMock = new Mock<IOne>();
iOneMock.Setup(x => x.Foo()).Returns(5);
// Let's set up the mock for ITwo when Foo is called with any string,
// it will return 1
var iTwoMock = new Mock<ITwo>();
iTwoMock.Setup(x => x.Foo(It.IsAny<string>())).Returns(1);
var some = new Some(iOneMock.Object, iTwoMock.Object);
// Act
some.Work();
// Assert
// Let's verify iOneMock.Foo was called.
iOneMock.Verify(x => x.Foo());
// Let's verify iTwoMock.Foo was called with string "One" and was called only once
iTwoMock.Verify(x => x.Foo("One"), Times.Once());
//排列
//让我们为IOne设置一个模拟,这样当调用Foo时,它将返回5
var iOneMock=new Mock();
Setup(x=>x.Foo())。返回(5);
//当使用任何字符串调用Foo时,让我们为ITwo设置mock,
//它将返回1
var iTwoMock=new Mock();
Setup(x=>x.Foo(It.IsAny())。返回(1);
var some=newsome(iOneMock.Object、iTwoMock.Object);
//表演
有些人工作;
//断言
//让我们来验证一下,Foo被叫来了。
iOneMock.Verify(x=>x.Foo());
//让我们验证一下iTwoMock.Foo是用字符串“One”调用的,并且只调用了一次
验证(x=>x.Foo(“一”),Times.Once();
在我上面的例子中,我试图展示带参数的方法,不带参数的方法,验证方法被调用,验证方法被调用一次。这会让你对可用的选项有更多的了解和想法。还有许多其他选择。有关更多信息,请参阅Moq文档。只需对每个依赖项的接口构造一个模拟,并将其传递给sut。模拟接口并将其传递给被测试的类。除了创建3个mock并将其注入构造器的参考之外,没有什么其他方法可以提供任何额外的帮助,这与大多数网站的玩具示例没有太大区别。