I'm attempting to test the following publishing-code (classes and properties are adjusted for example-purposes):
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;
});
}
The test-code (altered to match dummy-messages)
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");
This works just fine if the failedMessages
array only contains one message, but fails with the following error when the array has more than one message:
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.
Any ideas on how to use the ExpectPublish test (or something else) when testing multiple messages in one Publish?