NServicebus Test.Handler ExpectSend 未给出预期结果
我前段时间在 yahoogroup 上问过这个问题,但不幸的是没有答案。
当 ExpectSend 添加到单元测试时,测试失败并显示以下消息:
Rhino.Mocks.Exceptions.ExpectationViolationException: IBus.Send(callback method:
<>c__DisplayClass2`1.<ExpectSend>b__1); Expected #1, Actual #0
。
这看起来像 Bus.Send 方法从未被调用。但确实如此。 当不使用带有.ExpectSend的行时,测试成功。
[TestMethod()]
public void Should_Handle_Goedkeuring_Which_Results_In_VolledigGoedgekeurdeFactuur()
{
int inkoopFactuurId = 5;
Test.Initialize();
var mock = new Mock<IProcesGoedkeuringDataService>();
mock.Setup(x => x.IsFactuurVolledigGoedgekeurd(inkoopFactuurId)).Returns(true);
Test.Handler<GoedkeuringDoorGebruikerCommandHandler>()
.WithExternalDependencies(h => h.DataService = mock.Object)
.ExpectSend<FactuurVolledigGoedgekeurdCommand>(c =>
c.InkoopFactuurId == inkoopFactuurId)
.OnMessage<GoedkeuringDoorGebruikerCommand>(m =>
SetupMessage(m)); ;
}
处理程序:在本例中,IsFactuurVolledigGoedgekeurd 方法返回 true。
public class GoedkeuringDoorGebruikerCommandHandler : IHandleMessages<GoedkeuringDoorGebruikerCommand>
{
public IBus Bus { get; set; }
public IProcesGoedkeuringDataService DataService { get; set; }
public void Handle(GoedkeuringDoorGebruikerCommand message)
{
RegistreerGoedkeuring(message.InkoopFactuurId, message.GebruikerId);
bool volledigGoedgekeurd = IsFactuurVolledigGoedgekeurd(message.InkoopFactuurId);
if (volledigGoedgekeurd)
{
Bus.Send(new FactuurVolledigGoedgekeurdCommand(message.InkoopFactuurId));
}
}
}
I asked this some time ago on the yahoogroup but unfortunately no answer.
When ExpectSend is added to the unittest, the test fails with this message:
Rhino.Mocks.Exceptions.ExpectationViolationException: IBus.Send(callback method:
<>c__DisplayClass2`1.<ExpectSend>b__1); Expected #1, Actual #0
.
This looks like the Bus.Send method is never called. But it is.
When the line with .ExpectSend is not used, the test succeeded.
[TestMethod()]
public void Should_Handle_Goedkeuring_Which_Results_In_VolledigGoedgekeurdeFactuur()
{
int inkoopFactuurId = 5;
Test.Initialize();
var mock = new Mock<IProcesGoedkeuringDataService>();
mock.Setup(x => x.IsFactuurVolledigGoedgekeurd(inkoopFactuurId)).Returns(true);
Test.Handler<GoedkeuringDoorGebruikerCommandHandler>()
.WithExternalDependencies(h => h.DataService = mock.Object)
.ExpectSend<FactuurVolledigGoedgekeurdCommand>(c =>
c.InkoopFactuurId == inkoopFactuurId)
.OnMessage<GoedkeuringDoorGebruikerCommand>(m =>
SetupMessage(m)); ;
}
The handler: The IsFactuurVolledigGoedgekeurd method returns true in this case.
public class GoedkeuringDoorGebruikerCommandHandler : IHandleMessages<GoedkeuringDoorGebruikerCommand>
{
public IBus Bus { get; set; }
public IProcesGoedkeuringDataService DataService { get; set; }
public void Handle(GoedkeuringDoorGebruikerCommand message)
{
RegistreerGoedkeuring(message.InkoopFactuurId, message.GebruikerId);
bool volledigGoedgekeurd = IsFactuurVolledigGoedgekeurd(message.InkoopFactuurId);
if (volledigGoedgekeurd)
{
Bus.Send(new FactuurVolledigGoedgekeurdCommand(message.InkoopFactuurId));
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检查此代码(基于 RequestResponse NServiceBus 示例):
输出:
1. 通过
2. 失败
3. 失败
(Rhino.Mocks.Exceptions.ExpectationViolationException : IBus.Send(callback method: <>c_DisplayClass2`1.b_1); Expected #1, Actual #0.)
可能是使用您的方法作为 ExpectSend/OnMessage 方法的操作,它会破坏 RhinoMocks 的期望。我不知道为什么,但这就是出现异常的原因。
Check this code (based on RequestResponse NServiceBus sample):
Output:
1. Pass
2. Fail
3. Fail
(Rhino.Mocks.Exceptions.ExpectationViolationException : IBus.Send(callback method: <>c_DisplayClass2`1.b_1); Expected #1, Actual #0.)
Probably the using of your method applied as an action to ExpectSend/OnMessage method it breaks the RhinoMocks expectation. I do not know why, but it is the reason why exception appeared.