Unable to mock assynchronous service behaviour with Rhyno mock
I try to Mock (with Rhyno mock) the behaviour of a assynchronous service.
Here is an example: I got a service with one methode called 开发者_如何转开发void GetServerState(). As this method is assynchronous, it is void but when it's called, it'll call the proxies and the call an event GetServerStateCompleted(object,eventargs). At this point I hope everybody still follow me ;-)
now, let have a look at the mock (wich is infact a stub, but never mind)
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();
}
As everything seems right for me, this code doesn't work at all. Does someone has a begin of explanation for why it should not work ?
thx,
I found what was wrong:
public MyStub()
{
MockRepository = new MockRepository ();
//MyService = MockRepository.Stub<IMyService >(); //Stupid Stupid Stupid !!!
MyService = MockRepository.GenerateStub<IMyService >();
//And now, let's try to mock the behaviour
MyService.Stub(sm => sm.GetServerState())
.IgnoreArguments()
.Do((DelegateVoid)GetServerStateCompletedBehaviour);
}
精彩评论