我有这堂课:
class MyClass
{
private ISomeInterface blabla;
public MyClass() : this(new SomeInterfaceImplementation()) {}
internal MyClass(ISomeInterface blabla)
{
this.blabla = blabla;
}
public void SomeMethod(string id, int value1, int value2)
{
this.blabla.DoSomethingWith(id, new ValueClass(value1, value2))
}
}
我也有这个测试:
[TestFixture]
public class MyClassTest
{
private const string ID = "id";
private const int VALUE1 = 1;
private const int VALUE2 = 2;
private ValueClass valueClass;
private Mock<ISomeInterface> mockInterface;
private MyClass myClass;
[SetUp]
public void SetUp()
{
this.valueClass = new ValueClass(VALUE1, VALUE2);
this.mockInterface = new Mock<ISomeInterface>();
this.myClass = new MyClass(this.mockInterface.Object);
}
[Test]
public void GIVEN_AnID_AND_AValue1_AND_AValue2_WHEN_DoingSomeMethod_THEN_TheSomeInterfaceShouldDoSomething()
{
this.myClass.SomeMethod(ID, VALUE1, VALUE2);
this.mockInterface.Verify(m => m.DoSomethingWith(ID, this.valueClass), Times.Once()); //<- Test fails here!
}
}
我不知道为什么,但我无法通过此测试。NCrunch 给我以下错误信息:
Moq.MockException :预期在模拟上调用一次,但为 0 次:m => m.DoSomethingWith("ID", .valueClass) 未配置任何设置。
执行的调用:
ISomeInterface.DoSomethingWith("ID", MyNamespace.ValueClass) 在 Moq.Mock.ThrowVerifyException(MethodCall 预期, IEnumerable
1 setups, IEnumerable
1 actualCalls, Expression 表达式, Times times, Int32 callCount) 在 Moq.Mock.VerifyCalls(Interceptor targetInterceptor, MethodCall 预期, Expression 表达式, Times times) at Moq.Mock.Verify[T](Mock1 mock, Expression
1 expression, Times times, String failMessage) at Moq.Mock1.Verify(Expression
1 expression, Times times) at Tests.MyClassTest.GIVEN_AnID_AND_AValue1_AND_AValue2_WHEN_DoingSomeMethod_THEN_TheSomeInterfaceShouldDoSomething() 在 C:\MySourceCode\File 和行号在这里。
如您所见,Moq 似乎“没有看到”我的调用,可能是因为new ValueClass(value1, value2)
我怎样才能使这个测试通过或者我怎样才能改变我的设计以便更容易测试?我应该把它放在哪里new ValueClass(value1, value2)
?
编辑:
这是我应该在软件工程而不是 StackOverflow 上问的问题吗?这超出范围了吗?