Unit testing Dapper Update

dapper extension-methods moq nunit unit-testing

Question

Database.Setup(x => x.Update(It.IsAny<Subscription>()))
                    .Callback<object>(sub => SavedSubscription = sub as Subscription);

This line works with PetaPoco. Because of project requirements, had to switch to Dapper (and also had to add DapperExtensions in order to have CRUD methods) so now I am getting this error:

An expression tree may not contain a call or invocation that uses optional arguments

Any ideas how to handle this?

Popular Answer

You can't use Moq to mock methods that have optional parameters (as Update does). See this question for more details, though you're stuck specifying all parameters to the Update method.

Note that this is a limitation inherent to the framework and not Moq; you can't pass a method with a default parameter to an Expression. Jon Skeet gives a good demonstration why here.




Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why