无法使用 Rhyno 模拟来模拟异步服务行为
我尝试模拟(使用 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();
}
因为一切对我来说似乎都是正确的,所以这段代码根本不起作用。有人开始解释为什么它不起作用吗?
谢谢,
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,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现出了什么问题:
I found what was wrong: