获取一个被调用的Typemock模拟方法的参数

时间:2009-06-26 17:06:09

标签: mocking rhino-mocks typemock

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

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

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

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

亚当

1 个答案:

答案 0 :(得分: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.