如何使用 Moq 测试对 NServiceBus IBus.Publish() 的调用?
以下代码编译、运行,完全符合我的预期 - 当事件引发时,GreetingPublisher 调用bus.Publish() - 但 Moq 设置不匹配:
using Moq;
using NServiceBus;
using NUnit.Framework;
namespace MyProject.Greetifier.Tests {
[TestFixture]
public class GreetingPublisher_Bus_Integration_Tests {
[Test]
public void Greeting_Is_Published_To_Bus() {
var mockGreeter = new Mock<IGreeter>();
var mockBus = new Mock<IBus>();
mockBus.Setup(bus => bus.Publish<IMessage>(It.IsAny<IMessage>()))
.Verifiable();
var Greetifier = new GreetingPublisher(mockGreeter.Object,
mockBus.Object);
mockGreeter.Raise(m => m.Greet += null, "world");
mockBus.Verify();
}
}
public class HelloMessage : IMessage {
public string Name { get; set; }
public HelloMessage(string name) { this.Name = name; }
}
public class GreetingPublisher {
private readonly IGreeter greeter;
private readonly IBus bus;
public GreetingPublisher(IGreeter greeter, IBus bus) {
this.greeter = greeter;
greeter.Greet += handleGreetEvent;
this.bus = bus;
}
void handleGreetEvent(string name) {
bus.Publish(new HelloMessage(name));
}
}
public delegate void GreetingEvent(string name);
public interface IGreeter {
event GreetingEvent Greet;
}
}
并且在运行测试时,我得到:
Test 'MyProject.Greetifier.Tests.GreetingPublisher_Bus_Integration_Tests.Greeting_Is_Published_To_Bus' failed:
Moq.MockVerificationException : The following setups were not matched:
IBus bus => bus.Publish<IMessage>(new[] { It.IsAny<IMessage>() })
at Moq.Mock.Verify()
D:\Projects\MyProject\src\MyProject.Greetifier.Tests\Program.cs(15,0): MyProject.Greetifier.Tests.GreetingPublisher_Bus_Integration_Tests.Greeting_Is_Published_To_Bus()
我是吗缺少一些明显的东西?
The following code compiles, runs, does exactly what I'm expecting - the GreetingPublisher calls bus.Publish() when the event is raised - but the Moq setup isn't being matched:
using Moq;
using NServiceBus;
using NUnit.Framework;
namespace MyProject.Greetifier.Tests {
[TestFixture]
public class GreetingPublisher_Bus_Integration_Tests {
[Test]
public void Greeting_Is_Published_To_Bus() {
var mockGreeter = new Mock<IGreeter>();
var mockBus = new Mock<IBus>();
mockBus.Setup(bus => bus.Publish<IMessage>(It.IsAny<IMessage>()))
.Verifiable();
var Greetifier = new GreetingPublisher(mockGreeter.Object,
mockBus.Object);
mockGreeter.Raise(m => m.Greet += null, "world");
mockBus.Verify();
}
}
public class HelloMessage : IMessage {
public string Name { get; set; }
public HelloMessage(string name) { this.Name = name; }
}
public class GreetingPublisher {
private readonly IGreeter greeter;
private readonly IBus bus;
public GreetingPublisher(IGreeter greeter, IBus bus) {
this.greeter = greeter;
greeter.Greet += handleGreetEvent;
this.bus = bus;
}
void handleGreetEvent(string name) {
bus.Publish(new HelloMessage(name));
}
}
public delegate void GreetingEvent(string name);
public interface IGreeter {
event GreetingEvent Greet;
}
}
and when running the test, I get:
Test 'MyProject.Greetifier.Tests.GreetingPublisher_Bus_Integration_Tests.Greeting_Is_Published_To_Bus' failed:
Moq.MockVerificationException : The following setups were not matched:
IBus bus => bus.Publish<IMessage>(new[] { It.IsAny<IMessage>() })
at Moq.Mock.Verify()
D:\Projects\MyProject\src\MyProject.Greetifier.Tests\Program.cs(15,0): MyProject.Greetifier.Tests.GreetingPublisher_Bus_Integration_Tests.Greeting_Is_Published_To_Bus()
Am I missing something obvious?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我没看错的话,您的代码会调用
IBus.Publish
而不是IBus.Publish
。(编辑:我替换
为:
并且它按预期工作 - 迪伦)
If I see it correctly, your code calls
IBus.Publish<HelloMessage>
and notIBus.Publish<IMessage>
.(EDIT: I replaced
with:
and it works as expected - Dylan)