From f8583a24d5dc213499a10367c48d363a83793e14 Mon Sep 17 00:00:00 2001 From: Tim Bussmann Date: Mon, 26 Oct 2015 12:41:15 +0100 Subject: [PATCH 1/2] added assertions to test IBus.ForwardCurrentMessageTo(...) --- src/NServiceBus.Testing.Tests/SagaTests.cs | 37 ++++++- .../TestHandlerFixture.cs | 104 ++++++++++++++++++ src/NServiceBus.Testing/Handler.cs | 30 ++++- src/NServiceBus.Testing/Invocations.cs | 8 ++ src/NServiceBus.Testing/Saga.cs | 18 +++ 5 files changed, 190 insertions(+), 7 deletions(-) diff --git a/src/NServiceBus.Testing.Tests/SagaTests.cs b/src/NServiceBus.Testing.Tests/SagaTests.cs index 510d5e52..4a96b587 100644 --- a/src/NServiceBus.Testing.Tests/SagaTests.cs +++ b/src/NServiceBus.Testing.Tests/SagaTests.cs @@ -112,7 +112,6 @@ public void RemoteOrderWithAssertions() .When(s => s.Handle(new SubmitOrder { Total = total, IsRemoteOrder = true })); } - [Test] public void DiscountTestWithSpecificTimeout() { @@ -131,6 +130,7 @@ public void DiscountTestWithSpecificTimeout() .ExpectTimeoutToBeSetIn((state, span) => span == TimeSpan.FromDays(7)) .When(s => s.Handle(new SubmitOrder {Total = 200})); } + [Test] public void TestNullReferenceException() { @@ -138,6 +138,40 @@ public void TestNullReferenceException() var saga = new MySaga(); Assert.DoesNotThrow(() => Test.Saga(saga)); } + + [Test] + [ExpectedException] + public void ShouldFailExpectForwardCurrentMessageToIfMessageForwardedToUnexpectedDestination() + { + Test.Saga() + .ExpectForwardCurrentMessageTo(dest => dest == "expectedDestination") + .When(s => s.Handle(new StartsSaga())); + } + + [Test] + public void ShouldPassExpectForwardCurrentMessageToIfMessageForwardedToExpectedDestination() + { + Test.Saga() + .ExpectForwardCurrentMessageTo(dest => dest == "forwardingDestination") + .When(s => s.Handle(new StartsSaga())); + } + + [Test] + [ExpectedException] + public void ShouldFailExpectNotForwardCurrentMessageToIfMessageForwardedToExpectedDestination() + { + Test.Saga() + .ExpectNotForwardCurrentMessageTo(dest => dest == "forwardingDestination") + .When(s => s.Handle(new StartsSaga())); + } + + [Test] + public void ShouldPassExpectNotForwardCurrentMessageToIfMessageForwardedToUnexpectedDestination() + { + Test.Saga() + .ExpectNotForwardCurrentMessageTo(dest => dest == "expectedDestination") + .When(s => s.Handle(new StartsSaga())); + } } @@ -198,6 +232,7 @@ public void Handle(StartsSaga message) ReplyToOriginator(new ResponseToOriginator()); Bus.Publish(); Bus.Send(null); + Bus.ForwardCurrentMessageTo("forwardingDestination"); RequestTimeout(TimeSpan.FromDays(7), message); } diff --git a/src/NServiceBus.Testing.Tests/TestHandlerFixture.cs b/src/NServiceBus.Testing.Tests/TestHandlerFixture.cs index cb767f17..ff35043b 100644 --- a/src/NServiceBus.Testing.Tests/TestHandlerFixture.cs +++ b/src/NServiceBus.Testing.Tests/TestHandlerFixture.cs @@ -383,6 +383,76 @@ public void Should_be_able_to_pass_an_already_constructed_message_into_handler_w Assert.DoesNotThrow(() => Guid.Parse(handler.AssignedMessageId), "Message ID should be a valid GUID."); } + [Test] + [ExpectedException] + public void ShouldFailExpectForwardCurrentMessageToIfMessageNotForwarded() + { + Test.Handler() + .ExpectForwardCurrentMessageTo(dest => true) + .OnMessage(); + } + + [Test] + [ExpectedException] + public void ShouldFailExpectForwardCurrentMessageToIfMessageForwardedToUnexpectedDestination() + { + var handler = new ForwardingMessageHandler("someOtherDestination"); + + Test.Handler(handler) + .ExpectForwardCurrentMessageTo(dest => dest == "expectedDestination") + .OnMessage(); + } + + [Test] + public void ShouldPassExpectForwardCurrentMessageToIfMessageForwardedToExpectedDestination() + { + const string forwardingDestination = "expectedDestination"; + var handler = new ForwardingMessageHandler(forwardingDestination); + + Test.Handler(handler) + .ExpectForwardCurrentMessageTo(dest => dest == forwardingDestination) + .OnMessage(); + } + + [Test] + public void ShouldPassExpectNotForwardCurrentMessageToIfMessageNotForwarded() + { + Test.Handler() + .ExpectNotForwardCurrentMessageTo(dest => true) + .OnMessage(); + } + + [Test] + [ExpectedException] + public void ShouldFailExpectNotForwardCurrentMessageToIfMessageForwardedToExpectedDestination() + { + const string forwardingDestination = "expectedDestination"; + var handler = new ForwardingMessageHandler(forwardingDestination); + + Test.Handler(handler) + .ExpectNotForwardCurrentMessageTo(dest => dest == forwardingDestination) + .OnMessage(); + } + + [Test] + public void ShouldPassExpectNotForwardCurrentMessageToIfMessageForwardedToUnexpectedDestination() + { + var handler = new ForwardingMessageHandler("someOtherDestination"); + + Test.Handler(handler) + .ExpectNotForwardCurrentMessageTo(dest => dest == "expectedDestination") + .OnMessage(); + } + + [Test] + public void ExpectForwardCurrentMessageToShouldSupportMultipleForwardedMessages() + { + Test.Handler() + .ExpectForwardCurrentMessageTo(dest => dest == "dest1") + .ExpectForwardCurrentMessageTo(dest => dest == "dest2") + .ExpectNotForwardCurrentMessageTo(dest => dest == "dest3") + .OnMessage(); + } private class TestMessageImpl : TestMessage { @@ -587,6 +657,40 @@ void IHandleMessages.Handle(TestMessage message) } + public class NotForwardingMessageHandler : IHandleMessages + { + public void Handle(TestMessage message) + { + } + } + + public class ForwardingMessageHandler : IHandleMessages + { + readonly string destination; + + public ForwardingMessageHandler(string destination) + { + this.destination = destination; + } + + public IBus Bus { get; set; } + + public void Handle(TestMessage message) + { + Bus.ForwardCurrentMessageTo(destination); + } + } + + public class MultipleForwardingsMessageHandler : IHandleMessages + { + public IBus Bus { get; set; } + + public void Handle(TestMessage message) + { + Bus.ForwardCurrentMessageTo("dest1"); + Bus.ForwardCurrentMessageTo("dest2"); + } + } } public class DataBusMessageHandler : IHandleMessages diff --git a/src/NServiceBus.Testing/Handler.cs b/src/NServiceBus.Testing/Handler.cs index 45930bfb..affdb9b7 100644 --- a/src/NServiceBus.Testing/Handler.cs +++ b/src/NServiceBus.Testing/Handler.cs @@ -155,7 +155,7 @@ public Handler ExpectHandleCurrentMessageLater() } /// - /// Check that the handler sends a message of the given type to sites + /// Check that the handler sends a message of the given type to sites. /// public Handler ExpectSendToSites(Func, bool> check) { @@ -164,7 +164,7 @@ public Handler ExpectSendToSites(Func } /// - /// Check that the handler doesn't send a message of the given type to sites + /// Check that the handler doesn't send a message of the given type to sites. /// public Handler ExpectNotSendToSites(Func, bool> check) { @@ -173,7 +173,7 @@ public Handler ExpectNotSendToSites(Func - /// Check that the handler defers a message of the given type + /// Check that the handler defers a message of the given type. /// public Handler ExpectDefer(Func check) { @@ -182,7 +182,7 @@ public Handler ExpectDefer(Func check) } /// - /// Check that the handler defers a message of the given type + /// Check that the handler defers a message of the given type. /// public Handler ExpectDefer(Func check) { @@ -191,7 +191,7 @@ public Handler ExpectDefer(Func check) } /// - /// Check that the handler doesn't defer a message of the given type + /// Check that the handler doesn't defer a message of the given type. /// public Handler ExpectNotDefer(Func check) { @@ -200,7 +200,7 @@ public Handler ExpectNotDefer(Func check) } /// - /// Check that the handler doesn't defer a message of the given type + /// Check that the handler doesn't defer a message of the given type. /// public Handler ExpectNotDefer(Func check) { @@ -208,6 +208,24 @@ public Handler ExpectNotDefer(Func check) return this; } + /// + /// Check that the handler doesn't forward a message to the given destination. + /// + public Handler ExpectNotForwardCurrentMessageTo(Func check) + { + expectedInvocations.Add(new ExpectedNotForwardCurrentMessageToInvocation{ Check = check }); + return this; + } + + /// + /// Check that the handler forwards a message to the given destination. + /// + public Handler ExpectForwardCurrentMessageTo(Func check) + { + expectedInvocations.Add(new ExpectedForwardCurrentMessageToInvocation { Check = check }); + return this; + } + /// /// Activates the test that has been set up passing in the given message. /// diff --git a/src/NServiceBus.Testing/Invocations.cs b/src/NServiceBus.Testing/Invocations.cs index 2261c17f..b1446f67 100644 --- a/src/NServiceBus.Testing/Invocations.cs +++ b/src/NServiceBus.Testing/Invocations.cs @@ -119,6 +119,14 @@ class ExpectedReplyInvocation : SingleMessageExpectedInvocation : MessageInvocation { } class ForwardCurrentMessageToInvocation : SingleValueInvocation { } + class ExpectedForwardCurrentMessageToInvocation : SingleValueExpectedInvocation { } + class ExpectedNotForwardCurrentMessageToInvocation : SingleValueExpectedInvocation + { + public ExpectedNotForwardCurrentMessageToInvocation() + { + Negate = true; + } + } class ExpectedReturnInvocation : SingleValueExpectedInvocation, T> { } class ReturnInvocation : SingleValueInvocation { } diff --git a/src/NServiceBus.Testing/Saga.cs b/src/NServiceBus.Testing/Saga.cs index cbe6521e..75698d3e 100644 --- a/src/NServiceBus.Testing/Saga.cs +++ b/src/NServiceBus.Testing/Saga.cs @@ -188,6 +188,24 @@ public Saga ExpectSendToDestination(Action check return ExpectSendToDestination(CheckActionToFunc(check)); } + /// + /// Check that the saga doesn't forward a message to the given destination. + /// + public Saga ExpectNotForwardCurrentMessageTo(Func check) + { + expectedInvocations.Add(new ExpectedNotForwardCurrentMessageToInvocation { Check = check }); + return this; + } + + /// + /// Check that the saga forwards a message to the given destination. + /// + public Saga ExpectForwardCurrentMessageTo(Func check) + { + expectedInvocations.Add(new ExpectedForwardCurrentMessageToInvocation { Check = check }); + return this; + } + /// /// Check that the saga replies to the originator with the given message type. /// From 2e4aea6e8a65f7cac371bc9e207094545e648af3 Mon Sep 17 00:00:00 2001 From: Tim Bussmann Date: Mon, 26 Oct 2015 15:04:03 +0100 Subject: [PATCH 2/2] replaced ExpectedExceptionAttribute with Assert.Throws --- .../InvocationTests.cs | 24 ++--- src/NServiceBus.Testing.Tests/SagaTests.cs | 10 +- .../TestHandlerFixture.cs | 95 ++++++++----------- 3 files changed, 50 insertions(+), 79 deletions(-) diff --git a/src/NServiceBus.Testing.Tests/InvocationTests.cs b/src/NServiceBus.Testing.Tests/InvocationTests.cs index fa5df525..6ccee87c 100644 --- a/src/NServiceBus.Testing.Tests/InvocationTests.cs +++ b/src/NServiceBus.Testing.Tests/InvocationTests.cs @@ -25,33 +25,30 @@ public void PublishValuePositive() } [Test] - [ExpectedException] public void PublishBasicNegativeCheck() { var i = new PublishInvocation {Message = new MessageA()}; var exp = new ExpectedPublishInvocation {Check = m => false}; - exp.Validate(i); + Assert.Throws(() => exp.Validate(i)); } [Test] - [ExpectedException] public void PublishValueNegativeCheck() { var i = new PublishInvocation {Message = new MessageA {Value = 2}}; var exp = new ExpectedPublishInvocation {Check = m => m.Value == 3}; - exp.Validate(i); + Assert.Throws(() => exp.Validate(i)); } [Test] - [ExpectedException] public void PublishBasicNegativeType() { var i = new PublishInvocation {Message = new MessageA()}; var exp = new ExpectedPublishInvocation {Check = m => true}; - exp.Validate(i); + Assert.Throws(() => exp.Validate(i)); } [Test] @@ -75,33 +72,30 @@ public void PublishValueMultiplePositive() } [Test] - [ExpectedException] public void NotPublishBasicNegative() { var i = new PublishInvocation {Message = new MessageA()}; var exp = new ExpectedNotPublishInvocation {Check = m => true}; - exp.Validate(i); + Assert.Throws(() => exp.Validate(i)); } [Test] - [ExpectedException] public void SendPublishMismatchOne() { var i = new SendInvocation {Message = new MessageA()}; var exp = new ExpectedPublishInvocation {Check = m => true}; - exp.Validate(i); + Assert.Throws(() => exp.Validate(i)); } [Test] - [ExpectedException] public void SendPublishMismatchTwo() { var i = new PublishInvocation {Message = new MessageA()}; var exp = new ExpectedSendInvocation {Check = m => true}; - exp.Validate(i); + Assert.Throws(() => exp.Validate(i)); } [Test] @@ -133,23 +127,21 @@ public void SendToSitesBasicPositive() } [Test] - [ExpectedException] public void SendToSitesBasicNegativeCheck() { var i = new SendToSitesInvocation { Message = new MessageA() , Value = new[] { "SiteA" } }; var exp = new ExpectedSendToSitesInvocation { Check = (m, a) => false }; - exp.Validate(i); + Assert.Throws(() => exp.Validate(i)); } [Test] - [ExpectedException] public void NotSendToSitesBasicNegative() { var i = new SendToSitesInvocation { Message = new MessageA(), Value = new[] { "SiteA" } }; var exp = new ExpectedNotSendToSitesInvocation { Check = (m, a) => true }; - exp.Validate(i); + Assert.Throws(() => exp.Validate(i)); } [Test] diff --git a/src/NServiceBus.Testing.Tests/SagaTests.cs b/src/NServiceBus.Testing.Tests/SagaTests.cs index 4a96b587..dc5989a9 100644 --- a/src/NServiceBus.Testing.Tests/SagaTests.cs +++ b/src/NServiceBus.Testing.Tests/SagaTests.cs @@ -140,12 +140,11 @@ public void TestNullReferenceException() } [Test] - [ExpectedException] public void ShouldFailExpectForwardCurrentMessageToIfMessageForwardedToUnexpectedDestination() { - Test.Saga() + Assert.Throws(() => Test.Saga() .ExpectForwardCurrentMessageTo(dest => dest == "expectedDestination") - .When(s => s.Handle(new StartsSaga())); + .When(s => s.Handle(new StartsSaga()))); } [Test] @@ -157,12 +156,11 @@ public void ShouldPassExpectForwardCurrentMessageToIfMessageForwardedToExpectedD } [Test] - [ExpectedException] public void ShouldFailExpectNotForwardCurrentMessageToIfMessageForwardedToExpectedDestination() { - Test.Saga() + Assert.Throws(() => Test.Saga() .ExpectNotForwardCurrentMessageTo(dest => dest == "forwardingDestination") - .When(s => s.Handle(new StartsSaga())); + .When(s => s.Handle(new StartsSaga()))); } [Test] diff --git a/src/NServiceBus.Testing.Tests/TestHandlerFixture.cs b/src/NServiceBus.Testing.Tests/TestHandlerFixture.cs index ff35043b..63161406 100644 --- a/src/NServiceBus.Testing.Tests/TestHandlerFixture.cs +++ b/src/NServiceBus.Testing.Tests/TestHandlerFixture.cs @@ -15,12 +15,11 @@ public void ShouldAssertDoNotContinueDispatchingCurrentMessageToHandlersWasCalle } [Test] - [ExpectedException] public void ShouldFailAssertingDoNotContinueDispatchingCurrentMessageToHandlersWasCalled() { - Test.Handler() + Assert.Throws(() => Test.Handler() .ExpectDoNotContinueDispatchingCurrentMessageToHandlers() - .OnMessage(); + .OnMessage()); } [Test] @@ -32,12 +31,11 @@ public void ShouldAssertHandleCurrentMessageLaterWasCalled() } [Test] - [ExpectedException] public void ShouldFailAssertingSendToSitesWasCalled() { - Test.Handler() + Assert.Throws(() => Test.Handler() .ExpectSendToSites((m, a) => true) - .OnMessage(); + .OnMessage()); } [Test] @@ -69,23 +67,21 @@ public void ShouldAssertDeferWasCalledWithDateTime() } [Test] - [ExpectedException] public void ShouldFailAssertingDeferWasCalledWithTimeSpan() { var timespan = TimeSpan.FromMinutes(10); - Test.Handler() + Assert.Throws(() => Test.Handler() .ExpectDefer((m, t) => t == timespan) - .OnMessage(); + .OnMessage()); } [Test] - [ExpectedException] public void ShouldFailAssertingDeferWasCalledWithDateTime() { var datetime = DateTime.Now; - Test.Handler() + Assert.Throws(() => Test.Handler() .ExpectDefer((m, t) => t == datetime) - .OnMessage(); + .OnMessage()); } [Test] @@ -107,34 +103,31 @@ public void ShouldAssertDeferWasNotCalledWithDateTime() } [Test] - [ExpectedException] public void ShouldFailAssertingDeferWasNotCalledWithTimeSpan() { var timespan = TimeSpan.FromMinutes(10); - Test.Handler() + Assert.Throws(() => Test.Handler() .WithExternalDependencies(h => h.Defer = timespan) .ExpectNotDefer((m, t) => t == timespan) - .OnMessage(); + .OnMessage()); } [Test] - [ExpectedException] public void ShouldFailAssertingDeferWasNotCalledWithDateTime() { var datetime = DateTime.Now; - Test.Handler() + Assert.Throws(() => Test.Handler() .WithExternalDependencies(h => h.Defer = datetime) .ExpectNotDefer((m, t) => t == datetime) - .OnMessage(); + .OnMessage()); } [Test] - [ExpectedException] public void ShouldFailAssertingHandleCurrentMessageLaterWasCalled() { - Test.Handler() + Assert.Throws(() => Test.Handler() .ExpectHandleCurrentMessageLater() - .OnMessage(); + .OnMessage()); } [Test] @@ -155,12 +148,11 @@ public void ShouldPassExpectPublishWhenPublishing() } [Test] - [ExpectedException] public void ShouldFailExpectNotPublishWhenPublishing() { - Test.Handler>() + Assert.Throws(() => Test.Handler>() .ExpectNotPublish(m => true) - .OnMessage(); + .OnMessage()); } [Test] @@ -190,23 +182,21 @@ public void ShouldPassExpectPublishWhenPublishingAndCheckingPredicate() } [Test] - [ExpectedException] public void ShouldFailExpectNotPublishWhenPublishingAndCheckingPredicate() { - Test.Handler>() + Assert.Throws(() => Test.Handler>() .WithExternalDependencies(h => h.ModifyPublish = m => m.Data = "Data") .ExpectNotPublish(m => m.Data == "Data") - .OnMessage(); + .OnMessage()); } [Test] - [ExpectedException] public void ShouldFailExpectPublishWhenPublishingAndCheckingPredicateThatFails() { - Test.Handler>() + Assert.Throws(() => Test.Handler>() .WithExternalDependencies(h => h.ModifyPublish = m => m.Data = "NotData") .ExpectPublish(m => m.Data == "Data") - .OnMessage(); + .OnMessage()); } [Test] @@ -219,12 +209,11 @@ public void ShouldPassExpectNotPublishWhenPublishingAndCheckingPredicateThatFail } [Test] - [ExpectedException] public void ShouldFailExpectPublishIfNotPublishing() { - Test.Handler() + Assert.Throws(() => Test.Handler() .ExpectPublish(m => true) - .OnMessage(); + .OnMessage()); } [Test] @@ -244,21 +233,19 @@ public void ShouldPassExpectSendIfSending() } [Test] - [ExpectedException] public void ShouldFailExpectSendIfNotSending() { - Test.Handler() + Assert.Throws(() => Test.Handler() .ExpectSend(m => true) - .OnMessage(); + .OnMessage()); } [Test] - [ExpectedException] public void ShouldFailExpectSendIfSendingWithoutMatch() { - Test.Handler>() + Assert.Throws(() => Test.Handler>() .ExpectSend(m => true) - .OnMessage(); + .OnMessage()); } [Test] @@ -270,12 +257,11 @@ public void ShouldPassExpectNotSendIfNotSending() } [Test] - [ExpectedException] public void ShouldFailExpectNotSendIfSending() { - Test.Handler>() + Assert.Throws(() => Test.Handler>() .ExpectNotSend(m => true) - .OnMessage(); + .OnMessage()); } [Test] @@ -287,12 +273,11 @@ public void ShouldPassExpectNotSendLocalIfNotSendingLocal() } [Test] - [ExpectedException] public void ShouldFailExpectNotSendLocalIfSendingLocal() { - Test.Handler>() + Assert.Throws(() => Test.Handler>() .ExpectNotSendLocal(m => true) - .OnMessage(); + .OnMessage()); } [Test] @@ -304,12 +289,11 @@ public void ShouldPassExpectNotSendLocalIfSendingLocalWithoutMatch() } [Test] - [ExpectedException] public void ShouldFailExpectPublishIfPublishWrongMessageType() { - Test.Handler>() + Assert.Throws(() => Test.Handler>() .ExpectPublish(m => true) - .OnMessage(); + .OnMessage()); } [Test] @@ -384,23 +368,21 @@ public void Should_be_able_to_pass_an_already_constructed_message_into_handler_w } [Test] - [ExpectedException] public void ShouldFailExpectForwardCurrentMessageToIfMessageNotForwarded() { - Test.Handler() + Assert.Throws(() => Test.Handler() .ExpectForwardCurrentMessageTo(dest => true) - .OnMessage(); + .OnMessage()); } [Test] - [ExpectedException] public void ShouldFailExpectForwardCurrentMessageToIfMessageForwardedToUnexpectedDestination() { var handler = new ForwardingMessageHandler("someOtherDestination"); - Test.Handler(handler) + Assert.Throws(() => Test.Handler(handler) .ExpectForwardCurrentMessageTo(dest => dest == "expectedDestination") - .OnMessage(); + .OnMessage()); } [Test] @@ -423,15 +405,14 @@ public void ShouldPassExpectNotForwardCurrentMessageToIfMessageNotForwarded() } [Test] - [ExpectedException] public void ShouldFailExpectNotForwardCurrentMessageToIfMessageForwardedToExpectedDestination() { const string forwardingDestination = "expectedDestination"; var handler = new ForwardingMessageHandler(forwardingDestination); - Test.Handler(handler) + Assert.Throws(() => Test.Handler(handler) .ExpectNotForwardCurrentMessageTo(dest => dest == forwardingDestination) - .OnMessage(); + .OnMessage()); } [Test]