
var mocks = new MockRepository();var mockedInterface = mocks.CreateMock<IMyInterface>();Expect.Call(mockedInterface.FunctionThatReturn(param1,param2)).Return(Something);mockedInterface.FunctionReturningVoID(param3,param4);mocks.ReplayAll();// Some assert and other stuffmocks.VerifyAll();
这是正确的做法吗?我觉得它看起来很奇怪,因为你没有以同样的方式处理这两个函数.我想写的是:
var mocks = new MockRepository();var mockedInterface = mocks.CreateMock<IMyInterface>();Expect.Call(mockedInterface.FunctionThatReturn(param1,param2)).Return(Something);Expect.Call(mockedInterface.FunctionReturningVoID(param3,param4)); // This doesn't work.mocks.ReplayAll();// Some assert and other stuffmocks.VerifyAll();
但这不适用于第4行.我发现一些博客说你可以使用lambda(或委托)之类的
Expect.Call(() => mockedInterface.FunctionReturningVoID(param3,param4)); // This doesn't work.
但这对我来说似乎不起作用.拥有Expect.Call可以轻松识别模拟函数,这就是我想要它的原因.我得到的编译错误是:“无法将lambda表达式转换为类型’对象’,因为它不是委托类型”.
那怎么办呢?
更新:添加了编译错误信息.
解决方法 我更喜欢AAA(编号/动作/断言)语法而不是记录/重放.它更直接,使测试更容易阅读.你想要做的是:// arrangevar mock = MockRepository.GenerateMock<IMyInterface>mock.Expect(i => i.FunctionThatReturnSomething(param1,param2)).Return("hello");mock.Expect(i => i.FunctionThatReturnVoID(param3,param4));// set up other stuff for your code (like whatever code depends on IMyInterface)var foo = new Foo(mock);// actfoo.DoSomething();// assertmock.VerifyAll(); 总结 以上是内存溢出为你收集整理的c# – 在RhinoMocks中模拟void函数的正确方法是什么?全部内容,希望文章能够帮你解决c# – 在RhinoMocks中模拟void函数的正确方法是什么?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)