C# 断言这很有趣<;T>;被调用
给定以下类,当调用C# 断言这很有趣<;T>;被调用,c#,unit-testing,fakeiteasy,C#,Unit Testing,Fakeiteasy,给定以下类,当调用MethodUnderTest时,如何测试调用了GetSomething public class SystemUnderTest { private Foo foo; public string MethodUnderTest(int input) { return foo.Get(x => x.GetSomething(input)); } } 测试 public void VerifyGetSomethingInvo
MethodUnderTest
时,如何测试调用了GetSomething
public class SystemUnderTest
{
private Foo foo;
public string MethodUnderTest(int input)
{
return foo.Get(x => x.GetSomething(input));
}
}
测试
public void VerifyGetSomethingInvokedWhenMethodUnderTestIsInvoked()
{
//Arrange
var sut = new SystemUnderTest();
//Act
string unusedResult = sut.MethodUnderTest(5);
//Assert
A.CallTo(()=> sut.MethodUnderTest(A<int>.Ignored)) //Cant figure out how to test the Func<T> invocation
}
public void verifyGetSomethingingInvokedWhenMethodUnderTestisinVoked()验证
{
//安排
var sut=新系统未测试();
//表演
字符串unusedResult=sut.MethodUnderTest(5);
//断言
A.CallTo(()=>sut.MethodUnderTest(A.Ignored))//不知道如何测试Func调用
}
一般来说,使用假货进行测试的方法是
x
在您的代码中代表什么,但是您需要伪造任何类型的代码,并确保将伪造的代码提供给foo
实例。然后你会有类似的
public void verifyGetSomethingingInvokedWhenMethodUnderTestisinVoked()验证
{
//安排
var fakeX=A.Fake();
var sut=newsystemundertest(fakeX);//可能吧?哪个会将其传递给'foo'?
//表演
字符串unusedResult=sut.MethodUnderTest(5);
//断言
A.CallTo(()=>fakeX.GetSomething(5))。一定发生过();
}
谢谢。有时候,你只需要确认你所想的看起来很长,但这是一条路要走。由于实际代码中涉及大量泛型和委托,因此很难生成示例代码。您从我的示例代码中得到了要点,我非常感谢您提供的所有信息。我终于让它工作了。:)