Settings variable values in a Moq Callback call
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Callback() in Moq lets you run custom code when a mocked method is invoked, which makes it useful for capturing arguments, updating local test variables, or simulating side effects. The important detail is that the callback runs at invocation time, not at setup time, so any variables you assign there reflect the actual call made by the code under test.
Capture Arguments With Callback()
A common pattern is to save a method argument into a local variable for later assertions.
This is the standard answer to "how do I set variable values in a callback call": declare the variables outside the callback, then assign them inside the callback lambda.
Remember That The Variables Must Be In Outer Scope
The callback can only update variables that are visible to it.
This works:
Because callCount is declared outside the lambda.
That outer variable is captured by the closure, so you can inspect it after the invocation.
This is how you track:
- how many times a method was called
- which values were passed
- what the latest call looked like
Combine Returns And Callback
If the mocked method returns a value, you can still use a callback.
This is useful when you want both observable side effects in the test and a meaningful return value for the code under test.
Prefer Assertions Over Complex Test Logic
Callback() is helpful, but it should not become a mini-program inside the setup. If the callback becomes full of branching logic, the test often gets harder to understand than the production code it is meant to verify.
A good callback usually does one of these:
- capture arguments
- increment a counter
- mutate a tiny bit of test state
If you need much more, it may be a sign the test setup should be redesigned.
Sequence And Timing Matter
Because the callback runs when the method is invoked, the order of operations matters.
This is simple, but it explains a lot of confusion. The variable is not assigned at setup time. It changes only after the mock method is actually called.
Use Verify For Call Existence, Callback For Data Capture
Do not use Callback() for everything. If the only thing you need to check is that a call happened, Verify is usually clearer.
Use Callback() when you need to inspect or accumulate values dynamically, not just confirm the invocation occurred.
That distinction keeps tests easier to read.
Common Pitfalls
The biggest mistake is expecting the callback to run during setup. It only runs when the mocked method is actually invoked.
Another mistake is declaring the capture variable inside the callback instead of outside it. If the test cannot see the variable later, there is nothing useful to assert.
People also overuse Callback() when Verify() would be clearer and simpler.
Finally, if the code under test calls the mock from multiple threads, updating shared variables in callbacks may need synchronization. For normal unit tests this is uncommon, but it matters in concurrent code.
Summary
- '
Callback()runs when the mock method is invoked, not when the setup is defined.' - Capture variables by declaring them outside the callback and assigning them inside.
- Use generic callback overloads to access method arguments cleanly.
- Combine
Callback()withReturns()when you need both side effects and return values. - Prefer
Verify()when you only need to assert that a call happened.
Related reading
- Settings.settings vs. app.config in .NET desktop app
- Setup RabbitMQ consumer in ASP.NET Core application
- SetupSet is obsolete. In place of what?
- SFTP Libraries for .NET
- Should SpringRunner be used in Spring Boot with Junit 5
- Simple embedded Kafka test example with spring boot
- SGEN An attempt was made to load an assembly with an incorrect format
- Shared AssemblyInfo for uniform versioning across the solution

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.