如何使用 Moq 测试对 NServiceBus IBus.Publish() 的调用?

发布于 2024-11-25 06:24:31 字数 2052 浏览 1 评论 0原文

以下代码编译、运行,完全符合我的预期 - 当事件引发时,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 技术交流群。

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

发布评论

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

评论(1

若能看破又如何 2024-12-02 06:24:31

如果我没看错的话,您的代码会调用 IBus.Publish 而不是 IBus.Publish

(编辑:我替换

mockBus.Setup(bus => bus.Publish<IMessage>(It.IsAny<IMessage>()))
               .Verifiable();

为:

mockBus.Setup(bus => bus.Publish<HelloMessage>(It.IsAny<HelloMessage>()))
               .Verifiable();

并且它按预期工作 - 迪伦)

If I see it correctly, your code calls IBus.Publish<HelloMessage> and not IBus.Publish<IMessage>.

(EDIT: I replaced

mockBus.Setup(bus => bus.Publish<IMessage>(It.IsAny<IMessage>()))
               .Verifiable();

with:

mockBus.Setup(bus => bus.Publish<HelloMessage>(It.IsAny<HelloMessage>()))
               .Verifiable();

and it works as expected - Dylan)

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