@@ -53,49 +53,43 @@ public static IEventRecording WithSender(this IEventRecording eventRecording, ob
5353 }
5454
5555 /// <summary>
56- /// Asserts that at least one occurrence of the events had at least one of the arguments matching a predicate. Returns
57- /// only the events that matched that predicate.
56+ /// Asserts that at least one occurence of the events had one or more arguments of the expected
57+ /// type <typeparamref name="T"/> which matched the given predicate.
58+ /// Returns only the events that matched both type and optionally a predicate.
5859 /// </summary>
5960 public static IEventRecording WithArgs < T > ( this IEventRecording eventRecording , Expression < Func < T , bool > > predicate )
6061 {
6162 Guard . ThrowIfArgumentIsNull ( predicate , nameof ( predicate ) ) ;
6263
6364 Func < T , bool > compiledPredicate = predicate . Compile ( ) ;
6465
65- bool hasArgumentOfRightType = false ;
66- var eventsMatchingPredicate = new List < OccurredEvent > ( ) ;
66+ var eventsWithMatchingPredicate = new List < OccurredEvent > ( ) ;
6767
6868 foreach ( OccurredEvent @event in eventRecording )
6969 {
7070 var typedParameters = @event . Parameters . OfType < T > ( ) . ToArray ( ) ;
71- if ( typedParameters . Any ( ) )
72- {
73- hasArgumentOfRightType = true ;
74- }
7571
7672 if ( typedParameters . Any ( parameter => compiledPredicate ( parameter ) ) )
7773 {
78- eventsMatchingPredicate . Add ( @event ) ;
74+ eventsWithMatchingPredicate . Add ( @event ) ;
7975 }
8076 }
8177
82- if ( ! hasArgumentOfRightType )
83- {
84- throw new ArgumentException ( "No argument of event " + eventRecording . EventName + " is of type <" + typeof ( T ) + ">." ) ;
85- }
78+ bool foundMatchingEvent = eventsWithMatchingPredicate . Any ( ) ;
8679
87- if ( ! eventsMatchingPredicate . Any ( ) )
88- {
89- Execute . Assertion
90- . FailWith ( "Expected at least one event with arguments matching {0}, but found none." , predicate . Body ) ;
91- }
80+ Execute . Assertion
81+ . ForCondition ( foundMatchingEvent )
82+ . FailWith ( "Expected at least one event which arguments are of type <{0}> and matches {1}, but found none." ,
83+ typeof ( T ) ,
84+ predicate . Body ) ;
9285
93- return new FilteredEventRecording ( eventRecording , eventsMatchingPredicate ) ;
86+ return new FilteredEventRecording ( eventRecording , eventsWithMatchingPredicate ) ;
9487 }
9588
9689 /// <summary>
97- /// Asserts that at least one of the occurred events has arguments the match the predicates in the same order. Returns
98- /// only the events that matched those predicates.
90+ /// Asserts that at least one occurence of the events had one or more arguments of the expected
91+ /// type <typeparamref name="T"/> which matched the predicates in the same order.
92+ /// Returns only the events that matched both type and optionally predicates.
9993 /// </summary>
10094 /// <remarks>
10195 /// If a <c>null</c> is provided as predicate argument, the corresponding event parameter value is ignored.
@@ -104,49 +98,42 @@ public static IEventRecording WithArgs<T>(this IEventRecording eventRecording, p
10498 {
10599 Func < T , bool > [ ] compiledPredicates = predicates . Select ( p => p ? . Compile ( ) ) . ToArray ( ) ;
106100
107- bool hasArgumentOfRightType = false ;
108- var eventsMatchingPredicate = new List < OccurredEvent > ( ) ;
101+ var eventsWithMatchingPredicate = new List < OccurredEvent > ( ) ;
109102
110103 foreach ( OccurredEvent @event in eventRecording )
111104 {
112105 var typedParameters = @event . Parameters . OfType < T > ( ) . ToArray ( ) ;
113- if ( typedParameters . Any ( ) )
114- {
115- hasArgumentOfRightType = true ;
116- }
106+ bool hasArgumentOfRightType = typedParameters . Any ( ) ;
117107
118108 if ( predicates . Length > typedParameters . Length )
119109 {
120110 throw new ArgumentException (
121111 $ "Expected the event to have at least { predicates . Length } parameters of type { typeof ( T ) } , but only found { typedParameters . Length } .") ;
122112 }
123113
124- bool isMatch = true ;
114+ bool isMatch = hasArgumentOfRightType ;
125115 for ( int index = 0 ; index < predicates . Length && isMatch ; index ++ )
126116 {
127117 isMatch = compiledPredicates [ index ] ? . Invoke ( typedParameters [ index ] ) ?? true ;
128118 }
129119
130120 if ( isMatch )
131121 {
132- eventsMatchingPredicate . Add ( @event ) ;
122+ eventsWithMatchingPredicate . Add ( @event ) ;
133123 }
134124 }
135125
136- if ( ! hasArgumentOfRightType )
137- {
138- throw new ArgumentException ( "No argument of event " + eventRecording . EventName + " is of type <" + typeof ( T ) + ">." ) ;
139- }
126+ bool foundMatchingEvent = eventsWithMatchingPredicate . Any ( ) ;
140127
141- if ( ! eventsMatchingPredicate . Any ( ) )
128+ if ( ! foundMatchingEvent )
142129 {
143- Execute
144- . Assertion
145- . FailWith ( "Expected at least one event with arguments matching {0}, but found none." ,
146- string . Join ( " | " , predicates . Where ( p => p is not null ) . Select ( p => p . Body . ToString ( ) ) ) ) ;
130+ Execute . Assertion
131+ . FailWith ( "Expected at least one event which arguments are of type <{0}> and matches {1}, but found none." ,
132+ typeof ( T ) ,
133+ string . Join ( " | " , predicates . Where ( p => p is not null ) . Select ( p => p . Body . ToString ( ) ) ) ) ;
147134 }
148135
149- return new FilteredEventRecording ( eventRecording , eventsMatchingPredicate ) ;
136+ return new FilteredEventRecording ( eventRecording , eventsWithMatchingPredicate ) ;
150137 }
151138 }
152139}
0 commit comments