gpt4 book ai didi

unit-testing - 使用 Visual Studio 测试套件/NUnit 自动化集成测试 MSMQ 的最佳方法是什么?

转载 作者:行者123 更新时间:2023-12-02 09:39:52 25 4
gpt4 key购买 nike

我想为我正在编写的 MSMQ 应用程序创建一系列自动化单元测试。在我看来,挑战在于如何适应测试方法中的事件处理程序。也就是说,我从测试方法发送一条消息,并且需要将结果返回到该消息已被接收并处理的测试方法。我不知道如何实现这一点,任何指示将不胜感激。

最佳答案

您是否正在寻找一种编写单元测试的方法,其中被测系统认为它正在从队列接收事件,但您不想在测试期间使用真正的队列?

查看Rhino Mocks 。它允许您创建队列接口(interface)的模拟版本,然后在测试期间从中引发事件。用于测试 Requester.DoSomething() 方法的一些伪代码可能如下所示:

// SETUP
MockRepository mocks = new MockRepository();
IQueue mockQueue = mocks.StrictMock<IQueue>();

queue.Received+=null;//create an expectation that someone will subscribe to this event
LastCall.IgnoreArguments();// we don't care who is subscribing
IEventRaiser raiseReceivedEvent = LastCall.GetEventRaiser();//get event raiser for the last event, in this case, Received
Expect.Call(mockQueue.Send).Return(msgId);
mocks.ReplayAll();

// EXEC
Requester req = new Requester(mockQueue);

// We expect this method to send a request to the mock queue object.
req.DoSomething();
// Now we raise an event from the mock queue object.
raiseReceivedEvent.Raise(eventArgs);

// VERIFY
// we would probably also check some state in the Requester object
mocks.VerifyAll();

查看 Rhino mocks wiki了解所有详细信息。

关于unit-testing - 使用 Visual Studio 测试套件/NUnit 自动化集成测试 MSMQ 的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/831247/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com