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,