0
我尝试模拟(与Rhyno模拟)一个assynchronous服务的行为。无法模拟与Rhyno模拟同步服务行为模拟
这里是一个例子:我得到了一个叫做void GetServerState()的方法。由于这种方法是异步的,它是无效的,但是当它被调用时,它会调用代理并调用事件GetServerStateCompleted(object,eventargs)。 在这一点上我希望大家还是跟着我;-)
现在,让我们来看看模拟(至极是逸岸存根,但是没关系)
public class MyStub
{
protected MockRepository MockRepository {get;set;}
public IMyService MyService {get;set;} //the service with GetServerState() Method
protected delegate void DelegateVoid(); //for easy writting
public MyStub()
{
MockRepository = new MockRepository();
MyService = MockRepository.Stub<IMyService >();
//And now, let's try to mock the behaviour
MyService.Stub(sm => sm.GetServerState())
.IgnoreArguments()
.Do((DelegateVoid)GetServerStateCompletedBehaviour);
}
//the method that should be launched when someone call GetServerState on the Stub
protected void GetServerStateCompletedBehaviour()
{
MyService.Raise(x=>x.GetServerStateCompleted+=null,MyService,new EventArgs());
}
}
//And here is how I would like to use it
[TestMethod]
void Test()
{
try
{
IMyService Stub = new MyStub().MyService;
Stub += new EventHandler(EventMethod);
Stub.GetServerState();
Assert.Fail();
}
catch(MyException){}
}
void EventMethod(Object sender, EventArgs e)
{
Throw new MyException();
}
作为一切似乎适合我,这个代码根本不起作用。有人开始解释为什么它不应该起作用吗?
THX,