【发布时间】:2016-01-13 16:30:21
【问题描述】:
我有一个如下所示的接口和类:
public interface IServiceFacade
{
TResult Execute<TResult>(Func<IService, TResult> operation);
}
public class ServiceFacade : IServiceFacade
{
private readonly string endpoint = "EndPoint";
public TResult Execute<TResult>(Func<IService, TResult> operation)
{
// Call to remote WCF service that results in a TResult
return TResult;
}
}
IService 接口表示远程运行的 WCF 服务,因此该类中没有该接口的实现实例。
我这样调用这个方法两次:
public class ServiceConsumer
{
public ServiceConsumer(IServiceFacade serviceFacade)
{
var returnInteger1 = serviceFacade.Execute(service => service.Method1("StringArgument1"));
var returnInteger2 = serviceFacade.Execute(service => service.Method1("StringArgument2"));
}
}
在我的单元测试中,我想将第一次调用的返回值存根为 1,第二次调用为 2。
示例测试方法
[Test]
public void TestMethod()
{
var serviceFacadeStub = MockRepository.GenerateStub<IServiceFacade>();
serviceFacadeStub.Stub(call => call.Execute(Arg<Func<IService, int>.Matches(?))).Return(1);
serviceFacadeStub.Stub(call => call.Execute(Arg<Func<IService, int>.Matches(?))).Return(2);
var sut = new ServiceConsumer(serviceFacadeStub);
}
我不知道该在Matches 中输入什么,或者我最好使用除火柴之外的其他东西。
我现在正在使用 RhinoMocks 和 NUnit,但如果有更好的框架来做这件事,我愿意接受建议。
【问题讨论】:
标签: unit-testing lambda mocking rhino-mocks func