You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -111,6 +115,10 @@ foreach (var invocation in builder.Invocations)
111
115
112
116
**Callback Methods:**
113
117
-`OnCall(expression, callback)` - Execute logic when method is called
118
+
-`OnCall(expression, handler)` - Execute logic with no parameters when method is called
119
+
-`OnCall<T1>(expression, handler)` - Execute logic with strongly-typed first parameter
120
+
-`OnCall<T1, T2>(expression, handler)` - Execute logic with first two parameters
121
+
-`OnCall<T1, T2, T3>(expression, handler)` - Execute logic with first three parameters
114
122
-`OnCall(expression, matcher, callback)` - Execute logic when method is called with matching arguments
115
123
-`OnPropertyAccess<T>(property, callback)` - Execute logic on property get or set
116
124
-`OnGetCallback<T>(property, callback)` - Execute logic when property getter is accessed
@@ -121,21 +129,90 @@ foreach (var invocation in builder.Invocations)
121
129
-`Object` - Get the mock instance
122
130
-`Invocations` - Access all recorded invocations for custom verification
123
131
132
+
## Strongly-Typed Callbacks with Partial Parameters
133
+
134
+
BbQ.MockLite allows you to configure callbacks and return value handlers that receive only the parameters you care about, while maintaining full type safety.
135
+
136
+
### Setup with Strongly-Typed Handlers
137
+
138
+
Configure method return values using handlers that receive a subset of the method's parameters:
139
+
140
+
```csharp
141
+
usingBbQ.MockLite;
142
+
143
+
varbuilder=Mock.Create<IQueryService>();
144
+
145
+
// Handler receives only the first parameter (type-safe)
-**Type Safety**: Compile-time checking ensures parameter types match
192
+
-**Clean Code**: Only handle the parameters you care about, ignore the rest
193
+
-**IntelliSense Support**: Full IDE support with parameter names and types
194
+
-**Flexible**: Works with methods that have any number of parameters
195
+
124
196
## Callbacks for Custom Logic Execution
125
197
126
198
BbQ.MockLite supports callbacks that execute custom logic when methods are called or properties are accessed. This is useful for audit logging, state management, and complex verification scenarios.
127
199
128
200
```csharp
129
201
usingBbQ.MockLite;
130
202
131
-
// Track method calls with custom logic
203
+
// Track method calls with custom logic using strongly-typed handlers
132
204
varauditLog=newList<string>();
133
205
varbuilder=Mock.Create<IUserRepository>()
206
+
// Traditional approach with object array
134
207
.OnCall(x=>x.GetUser(It.IsAny<string>()),
135
208
args=>auditLog.Add($"GetUser called with: {args[0]}"))
209
+
// Strongly-typed approach (new)
136
210
.OnCall(x=>x.SaveUser(It.IsAny<User>()),
137
-
args=>args[0] isUseru&&u.IsAdmin,
138
-
args=>auditLog.Add($"Admin user saved: {((User)args[0]).Name}"));
211
+
(Useruser) =>auditLog.Add($"SaveUser called with: {user.Name}"))
212
+
// Conditional callback with matcher
213
+
.OnCall(x=>x.DeleteUser(It.IsAny<string>()),
214
+
args=>args[0] isstringid&&id.StartsWith("admin"),
215
+
args=>auditLog.Add($"Admin user deleted: {args[0]}"));
0 commit comments