当Publish(params T [] messages)包含多条消息时,ExpectPublish失败

时间:2012-06-20 11:21:07

标签: c# testing nunit rhino-mocks nservicebus

我正在尝试测试以下发布代码(为了示例目的而调整了类和属性):

public void PublishMessages(List<SomeDummyClass> shareRegistrationResult)
{    
    var failedMessages = shareRegistrationResult
                                .Where(c => !c.WasRegistered)
                                .Select(c => CreateNotRegisteredMessageInstance(c.String1, c.String2)).ToArray();
    _bus.Publish(failedMessages);
}

private IMyMessage CreateNotRegisteredMessageInstance(string string1, string string2)
{
    return _bus.CreateInstance<IMyMessage>(message =>
                                    {
                                        message.String1 = string1;
                                        message.String2 = string2;
                                    });
}

测试代码(改为匹配虚拟消息)

NServiceBus.Testing.Test.Handler(bus => new Publisher()
     .ExpectPublish<IMyMessage>(message => message.String1 == "Hello" && message.String2 == "World")
     .ExpectPublish<IMyMessage>(message => message.String1 == "Foo" && message.String2 == "Bar");

如果failedMessages数组只包含一条消息,但在数组有多条消息时失败并出现以下错误,则此方法正常工作:

Rhino.Mocks.Exceptions.ExpectationViolationException : IBus.Publish(callback method: <>c__DisplayClass1c`1.<ExpectPublish>b__1b); Expected #1, Actual #0.
IBus.Publish(callback method: <>c__DisplayClass1c`1.<ExpectPublish>b__1b); Expected #1, Actual #0.

在一次发布中测试多条消息时,如何使用ExpectPublish测试(或其他)的任何想法?

1 个答案:

答案 0 :(得分:0)