@@ -53,49 +53,43 @@ public static IEventRecording WithSender(this IEventRecording eventRecording, ob
53
53
}
54
54
55
55
/// <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.
58
59
/// </summary>
59
60
public static IEventRecording WithArgs < T > ( this IEventRecording eventRecording , Expression < Func < T , bool > > predicate )
60
61
{
61
62
Guard . ThrowIfArgumentIsNull ( predicate , nameof ( predicate ) ) ;
62
63
63
64
Func < T , bool > compiledPredicate = predicate . Compile ( ) ;
64
65
65
- bool hasArgumentOfRightType = false ;
66
- var eventsMatchingPredicate = new List < OccurredEvent > ( ) ;
66
+ var eventsWithMatchingPredicate = new List < OccurredEvent > ( ) ;
67
67
68
68
foreach ( OccurredEvent @event in eventRecording )
69
69
{
70
70
var typedParameters = @event . Parameters . OfType < T > ( ) . ToArray ( ) ;
71
- if ( typedParameters . Any ( ) )
72
- {
73
- hasArgumentOfRightType = true ;
74
- }
75
71
76
72
if ( typedParameters . Any ( parameter => compiledPredicate ( parameter ) ) )
77
73
{
78
- eventsMatchingPredicate . Add ( @event ) ;
74
+ eventsWithMatchingPredicate . Add ( @event ) ;
79
75
}
80
76
}
81
77
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 ( ) ;
86
79
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 ) ;
92
85
93
- return new FilteredEventRecording ( eventRecording , eventsMatchingPredicate ) ;
86
+ return new FilteredEventRecording ( eventRecording , eventsWithMatchingPredicate ) ;
94
87
}
95
88
96
89
/// <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.
99
93
/// </summary>
100
94
/// <remarks>
101
95
/// 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
104
98
{
105
99
Func < T , bool > [ ] compiledPredicates = predicates . Select ( p => p ? . Compile ( ) ) . ToArray ( ) ;
106
100
107
- bool hasArgumentOfRightType = false ;
108
- var eventsMatchingPredicate = new List < OccurredEvent > ( ) ;
101
+ var eventsWithMatchingPredicate = new List < OccurredEvent > ( ) ;
109
102
110
103
foreach ( OccurredEvent @event in eventRecording )
111
104
{
112
105
var typedParameters = @event . Parameters . OfType < T > ( ) . ToArray ( ) ;
113
- if ( typedParameters . Any ( ) )
114
- {
115
- hasArgumentOfRightType = true ;
116
- }
106
+ bool hasArgumentOfRightType = typedParameters . Any ( ) ;
117
107
118
108
if ( predicates . Length > typedParameters . Length )
119
109
{
120
110
throw new ArgumentException (
121
111
$ "Expected the event to have at least { predicates . Length } parameters of type { typeof ( T ) } , but only found { typedParameters . Length } .") ;
122
112
}
123
113
124
- bool isMatch = true ;
114
+ bool isMatch = hasArgumentOfRightType ;
125
115
for ( int index = 0 ; index < predicates . Length && isMatch ; index ++ )
126
116
{
127
117
isMatch = compiledPredicates [ index ] ? . Invoke ( typedParameters [ index ] ) ?? true ;
128
118
}
129
119
130
120
if ( isMatch )
131
121
{
132
- eventsMatchingPredicate . Add ( @event ) ;
122
+ eventsWithMatchingPredicate . Add ( @event ) ;
133
123
}
134
124
}
135
125
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 ( ) ;
140
127
141
- if ( ! eventsMatchingPredicate . Any ( ) )
128
+ if ( ! foundMatchingEvent )
142
129
{
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 ( ) ) ) ) ;
147
134
}
148
135
149
- return new FilteredEventRecording ( eventRecording , eventsMatchingPredicate ) ;
136
+ return new FilteredEventRecording ( eventRecording , eventsWithMatchingPredicate ) ;
150
137
}
151
138
}
152
139
}
0 commit comments