无法使用 Rhyno 模拟来模拟异步服务行为

发布于 2024-10-20 14:27:43 字数 1399 浏览 0 评论 0原文

我尝试模拟(使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

疏忽 2024-10-27 14:27:44

我发现出了什么问题:

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);
 }

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);
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文