1

我是 RhinoMocks 的忠实用户,从 TDD 和 AAA 的角度使用 NUnit 和 ReSharper 进行开发。我正在换工作,我要搬到的团队使用 TypeMock,所以我想开始工作......我遇到了一个问题。如何获取模拟对象上被调用方法的参数。使用 RhinoMocks 时,我使用:

mockObject.GetArgumentsForCallsMadeOn(x => x.MethodIWantToGetParametersFrom(null))

它返回一个 IList 类型的对象数组。伟大的!我去得到我想要的东西并按照我的意愿处理它。现在使用 TypeMock 的 AAA 语法,我似乎无法找到一种方法来做到这一点......有人可以对此有所了解吗?我应该采取不同的做法吗?

感谢您的阅读,期待您的回复!

亚当

4

1 回答 1

1

你可以使用 DoInstead():

Isolate.WhenCalled(()=>x.MethodIWantToGetParametersFrom).DoInstead(context => Console.WriteLine(context.Parameters[0].ToString())

您将获得一个包含参数值的 Context 对象。

您还可以在自己的类上实现同名的方法,并将伪造对象的调用交换到该方法:

 class MyOwnClass
    {
    void MethodIWantTOGetParametersFrom(string s){
Console.WriteLine(s);
} //this is NOT the real method
    }

    //in test:
    MyOwnClass own = new MyOwnClass();
    Isolate.Swap.CallsOn(realClassInstance).WithCallsTo(own); //only methods that are implemented in the OwnCalss will be redirected. others will be called on the original instance.
于 2009-06-26T20:50:07.557 回答