[英]Mocking InvokeOperation for Unit Testing using Rhino Mocks
I have a Data Service interface with a method: 我有一个方法的数据服务接口:
void GetDataByLocationId(Guid locationId, Action<InvokeOperation<IEnumerable<DataValue>>> callback);
I want to test a class that depends on this data service interface, and would like to mock out the above method. 我想测试一个依赖于此数据服务接口的类,并想模拟出上述方法。
I set up my mocks as: 我将模拟设置为:
_mocks = new MockRepository();
var dataLoadOperation = _mocks.StrictMock<InvokeOperation<IEnumerable<DataValue>>>();
Action<InvokeOperation<IEnumerable<DataValue>>> dataValueCallback = null;
Then in my test execution: 然后在我的测试执行中:
var locationId = Guid.NewGuid();
var values = // something //
using (_mocks.Unordered())
{
Expect.Call(() => _dataService.GetDataByLocationId(Arg<Guid>.Is.Equal(locationId), Arg<Action<InvokeOperation<IEnumerable<DataValue>>>>.Is.Anything));
LastCall.Callback(new Func<Action<InvokeOperation<IEnumerable<DataValue>>>, bool>(c => { dataValueCallback = c; return true; }));
dataLoadOperation .Stub(x => x.HasError).Return(false);
dataLoadOperation .Stub(x => x.Value).Return(values);
}
But it never makes it to the execution... It turns out that: 但是它永远不会执行...事实证明:
System.ServiceModel.DomainServices.Client.InvokeOperation
System.ServiceModel.DomainServices.Client.InvokeOperation
Is a sealed class, and Rhino Mocks throws an Exception when running 是一个密封的类,Rhino Mocks在运行时抛出异常
Can't create mocks of sealed classes
无法创建密封类的模拟
I am looking for suggestions on improving my design to support this test case; 我正在寻找改进设计以支持该测试用例的建议; and still using mocks.
仍在使用模拟。
I would create an interface that represents your interaction with the InvokeOperation class. 我将创建一个接口来表示您与InvokeOperation类的交互。 Create a "default" implementation that simply delegates to the real InvokeOperation class.
创建一个“默认”实现,该实现简单地委派给实际的InvokeOperation类。 You can then stub out the interface during mocking.
然后,您可以在模拟过程中将接口存根。
The interface doesn't need to be a full copy of InvokeOperation's members and properties -- just the ones you use in your application. 该接口不需要是InvokeOperation的成员和属性的完整副本,而只是您在应用程序中使用的成员和属性的完整副本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.