我需要测试执行函数的逻辑,之后它会更改参数的属性,然后使用更新的参数执行相同的函数。
为了帮助说明这一点,这里有一些示例代码:
接口:
public interface IWorker
{
MyObjectB DoWork(MyObject myObject);
}
我的对象B:
public class MyObjectB
{
public string Message { get; set; }
}
执行:
public MyObjectB DoWork(IWorker worker, MyObject myObject)
{
worker.DoWork(myObject);
myObject.Name = "PersonB";
worker.DoWork(myObject);
return new MyObjectB() { Message = "Done" };
}
测试:
public void RhinoMocksSampleTest()
{
var workerStub = MockRepository.GenerateStub<IWorker>();
workerStub.Stub(n => n.DoWork(Arg<MyObject>.Is.Anything));
var myObj = new MyObject { Id = 1, Name = "PersonA" };
var p = new Program();
p.DoWork(workerStub, myObj);
workerStub.AssertWasCalled(d => d.DoWork(Arg<MyObject>.Matches(r => r.Name == "PersonA")));
workerStub.AssertWasCalled(d => d.DoWork(Arg<MyObject>.Matches(r => r.Name == "PersonB")));
}
第一个 AssertWasCalled 失败,因为值为“PersonB”。是否有一个函数/调用可以用来测试第一次调用的对象状态?