File tree Expand file tree Collapse file tree 6 files changed +77
-37
lines changed
NServiceBus.Testing.Tests Expand file tree Collapse file tree 6 files changed +77
-37
lines changed Original file line number Diff line number Diff line change @@ -170,6 +170,46 @@ public void ShouldPassExpectNotForwardCurrentMessageToIfMessageForwardedToUnexpe
170170 . ExpectNotForwardCurrentMessageTo ( dest => dest == "expectedDestination" )
171171 . When ( s => s . Handle ( new StartsSaga ( ) ) ) ;
172172 }
173+
174+ [ Test ]
175+ public void ShouldFailExpectReplyToOriginatorIfNotRepliedToOriginator ( )
176+ {
177+ Assert . Throws < Exception > ( ( ) => Test . Saga < SagaThatDoesAReply > ( )
178+ . ExpectReplyToOriginator < ResponseToOriginator > ( )
179+ . When ( s => s . Handle ( new MyRequest ( ) ) ) ) ;
180+ }
181+
182+ [ Test ]
183+ public void ShouldFailExpectNotReplyToOriginatorIfRepliedToOriginator ( )
184+ {
185+ Assert . Throws < Exception > ( ( ) => Test . Saga < MySaga > ( )
186+ . ExpectNotReplyToOriginator < ResponseToOriginator > ( )
187+ . When ( s => s . Handle ( new StartsSaga ( ) ) ) ) ;
188+ }
189+
190+ [ Test ]
191+ public void ShouldPassExpectNotReplyToOriginatorIfUsingReply ( )
192+ {
193+ Test . Saga < SagaThatDoesAReply > ( )
194+ . ExpectNotReplyToOriginator < ResponseToOriginator > ( )
195+ . When ( s => s . Handle ( new MyRequest ( ) ) ) ;
196+ }
197+
198+ [ Test ]
199+ public void ShouldPassExpectNotReplyToOriginatorIfNotReplyingToOriginator ( )
200+ {
201+ Test . Saga < MySagaWithInterface > ( )
202+ . ExpectNotReplyToOriginator < ResponseToOriginator > ( )
203+ . WhenHandling < StartsSagaWithInterface > ( ) ;
204+ }
205+
206+ [ Test ]
207+ public void ShouldPassExpectNotReplyToOriginatorIfFailingCheck ( )
208+ {
209+ Test . Saga < MySaga > ( )
210+ . ExpectNotReplyToOriginator < ResponseToOriginator > ( m => false )
211+ . When ( s => s . Handle ( new StartsSaga ( ) ) ) ;
212+ }
173213 }
174214
175215
Original file line number Diff line number Diff line change @@ -30,22 +30,6 @@ public void ShouldAssertHandleCurrentMessageLaterWasCalled()
3030 . OnMessage < TestMessage > ( ) ;
3131 }
3232
33- [ Test ]
34- public void ShouldFailAssertingSendToSitesWasCalled ( )
35- {
36- Assert . Throws < Exception > ( ( ) => Test . Handler < EmptyHandler > ( )
37- . ExpectSendToSites < TestMessage > ( ( m , a ) => true )
38- . OnMessage < TestMessage > ( ) ) ;
39- }
40-
41- [ Test ]
42- public void ShouldAssertSendToSitesWasNotCalled ( )
43- {
44- Test . Handler < EmptyHandler > ( )
45- . ExpectNotSendToSites < TestMessage > ( ( m , a ) => true )
46- . OnMessage < TestMessage > ( ) ;
47- }
48-
4933 [ Test ]
5034 public void ShouldAssertDeferWasCalledWithTimeSpan ( )
5135 {
Original file line number Diff line number Diff line change @@ -154,24 +154,6 @@ public Handler<T> ExpectHandleCurrentMessageLater()
154154 return this ;
155155 }
156156
157- /// <summary>
158- /// Check that the handler sends a message of the given type to sites.
159- /// </summary>
160- public Handler < T > ExpectSendToSites < TMessage > ( Func < TMessage , IEnumerable < string > , bool > check )
161- {
162- expectedInvocations . Add ( new ExpectedSendToSitesInvocation < TMessage > { Check = check } ) ;
163- return this ;
164- }
165-
166- /// <summary>
167- /// Check that the handler doesn't send a message of the given type to sites.
168- /// </summary>
169- public Handler < T > ExpectNotSendToSites < TMessage > ( Func < TMessage , IEnumerable < string > , bool > check )
170- {
171- expectedInvocations . Add ( new ExpectedNotSendToSitesInvocation < TMessage > { Check = check } ) ;
172- return this ;
173- }
174-
175157 /// <summary>
176158 /// Check that the handler defers a message of the given type.
177159 /// </summary>
Original file line number Diff line number Diff line change @@ -199,6 +199,11 @@ public ExpectedNotDeferMessageInvocation()
199199
200200 class ExpectedReplyToOriginatorInvocation < M > : ExpectedInvocation < ReplyToOriginatorInvocation < M > >
201201 {
202+ public ExpectedReplyToOriginatorInvocation ( bool negate = false )
203+ {
204+ Negate = negate ;
205+ }
206+
202207 public Func < M , Address , string , bool > Check { get ; set ; }
203208
204209 protected override bool Validate ( ReplyToOriginatorInvocation < M > invocation )
Original file line number Diff line number Diff line change @@ -7,7 +7,7 @@ namespace NServiceBus.Testing
77 /// <summary>
88 /// Saga unit testing framework.
99 /// </summary>
10- public class Saga < T > where T : Saga , new ( )
10+ public class Saga < T > where T : Saga
1111 {
1212 private readonly T saga ;
1313 private readonly StubBus bus ;
@@ -249,7 +249,36 @@ public Saga<T> ExpectReplyToOriginator<TMessage>(Action<TMessage> check)
249249 {
250250 return ExpectReplyToOriginator ( CheckActionToFunc ( check ) ) ;
251251 }
252-
252+
253+ /// <summary>
254+ /// Check that the saga does not reply to the originator with the given message type.
255+ /// </summary>
256+ public Saga < T > ExpectNotReplyToOriginator < TMessage > ( Func < TMessage , bool > check = null )
257+ {
258+ expectedInvocations . Add ( new ExpectedReplyToOriginatorInvocation < TMessage > ( negate : true )
259+ {
260+ Check = ( message , address , correlationId ) =>
261+ {
262+ if ( address == Address . Parse ( saga . Entity . Originator ) && correlationId == saga . Entity . OriginalMessageId )
263+ {
264+ check = check ?? ( m => true ) ;
265+ return check ( message ) ;
266+ }
267+
268+ return true ;
269+ }
270+ } ) ;
271+ return this ;
272+ }
273+
274+ /// <summary>
275+ /// Check that the saga does not reply to the originator with the given message type.
276+ /// </summary>
277+ public Saga < T > ExpectNotReplyToOriginator < TMessage > ( Action < TMessage > check )
278+ {
279+ return ExpectNotReplyToOriginator ( CheckActionToFunc ( check ) ) ;
280+ }
281+
253282 /// <summary>
254283 /// Check that the saga publishes a message of the given type complying with the given predicate.
255284 /// </summary>
Original file line number Diff line number Diff line change @@ -143,7 +143,7 @@ public static void Initialize(params Type[] types)
143143 /// Begin the test script for the passed in saga instance.
144144 /// Callers need to instantiate the saga's data class as well as give it an ID.
145145 /// </summary>
146- public static Saga < T > Saga < T > ( T saga ) where T : Saga , new ( )
146+ public static Saga < T > Saga < T > ( T saga ) where T : Saga
147147 {
148148 bus = new StubBus ( messageCreator ) ;
149149
You can’t perform that action at this time.
0 commit comments