我有一个方法可以调用同一个类中的方法,如下所示:
public class ClassSample {
public MethodA() {
var objectA = new ObjectA();
// do somethings with objectA
return MethodB(objectA)
}
public MethodB(ObjectA objectA) {
// do somethings
}
}
从上面的代码块中,如果我想在方法A中执行某些操作后测试,那么在方法B中使用正确的参数。
答案 0 :(得分:0)
您可以将MethodB
设置为可验证,
mock.Setup(p => p.MethodB(It.Is<ObjectA>(q => q.status == true))).Verifiable();
检查以下代码段,
public class ObjectA
{
public ObjectA()
{
}
public bool status { get; set; }
}
public class ClassSample
{
public ObjectA MethodA()
{
var objectA = new ObjectA();
// do somethings with objectA
objectA.status = true;
return MethodB(objectA);
}
public ObjectA MethodB(ObjectA objectA)
{
// do somethings
return objectA;
}
}
[TestMethod]
public void TestClassSample()
{
var mock = new Mock<ClassSample>();
mock.Setup(p => p.MethodB(It.Is<ObjectA>(q => q.status == true))).Verifiable();
var result = mock.Object.MethodA();
mock.Verify();
}