sti*_*k81 7 c# unit-testing rhino-mocks mocking
我正在使用RhinoMocks编写单元测试进行模拟,现在我需要一些我之前没有使用过的新功能.
我想调用一个函数,它再次调用异步函数.为了模拟async函数完成并使用执行结果触发给定的回调,我假设我可以在RhinoMocks中使用Callback功能,但是我该怎么做呢?
基本上我想做的是这样的事情:
fakeSomething = MockRepository.GenerateMock<ISomething>();
fakeSomething.FictionousFunctionSettingCallback(MyFunctionCalled, MyCallback, theParameterToGiveCallback);
var myObj = new MyClass(fakeSomething);
myObj.Foo();
// Foo() now fires the function MyFunctionCalled asynchronous,
// and eventually would trigger some callback
Run Code Online (Sandbox Code Playgroud)
所以; 是否有一个真正的功能我可以用这个"FictionousFunction"代替它?请问这是否不清楚..
Ste*_*ger 14
只需使用以下命令WhenCalled:
fakeSomething = MockRepository.GenerateMock<ISomething>();
fakeSomething
.Stub(x => x.Foo())
.WhenCalled(call => /* do whatever you want */);
Run Code Online (Sandbox Code Playgroud)
例如,您可以使用call参数的Arguments属性:
fakeSomething
.Stub(x => x.Foo(Arg<int>.Is.Anything))
.WhenCalled(call =>
{
int myArg = (int)call.Arguments[0]; // first arg is an int
DoStuff(myArg);
});
Run Code Online (Sandbox Code Playgroud)
它不是异步的.你最有可能不需要它是异步的,如果不是这样的话,它会使你的生活更轻松.
| 归档时间: |
|
| 查看次数: |
5172 次 |
| 最近记录: |