It's a comment rather than a new issue because I have seen many problems in this list that are the same as mine and have been open for years. And now it's less chance that they can be solved quickly.
I suggest everyone look at this article because its approach of "Wrap the Dapper" works really well for all these cases with only minimal code change. So instead of fixing specific bugs for different edge cases in this Moq.Dapper, you now have a simple and systematic pattern and can use Moq directly to Moq every Dapper method.
There are 2 steps in this article:
First, You create a DapperWrapper class & an IDapperWrapper interface. That's very simple because you can just copy-paste each of the Dapper's static query‘s signature twice (paste one in Interface and another in DapperWrapper class) then make minor changes to add "connection" as the 1st argument. I assume you only use several types of methods in your project (to me it's less than 5, say Query, QueryFirst, Execute, Query<T1, T2..>). You can get the method's signature easily from Dapper's source code.
Then, change the way of executing Dapper method in your code
From:
“
connection.Query(sql, param, buffer......)
”
To:
“
new DapperWrapper wrapper; // Create a dapper wrapper instance
wrapper.Query(connection, sql, param, buffer.....); // use this wrapper instance wherever you execute the Dapper methods and move “connection” from the beginning to your method’s 1st input argument, everything else is the same.
”
Then you can use Moq to create a Moked DapperWrapper object now and set up methods with Moq directly.
It's a comment rather than a new issue because I have seen many problems in this list that are the same as mine and have been open for years. And now it's less chance that they can be solved quickly.
I suggest everyone look at this article because its approach of "Wrap the Dapper" works really well for all these cases with only minimal code change. So instead of fixing specific bugs for different edge cases in this Moq.Dapper, you now have a simple and systematic pattern and can use Moq directly to Moq every Dapper method.
https://makolyte.com/csharp-how-to-unit-test-code-that-uses-dapper/
There are 2 steps in this article: First, You create a DapperWrapper class & an IDapperWrapper interface. That's very simple because you can just copy-paste each of the Dapper's static query‘s signature twice (paste one in Interface and another in DapperWrapper class) then make minor changes to add "connection" as the 1st argument. I assume you only use several types of methods in your project (to me it's less than 5, say Query, QueryFirst, Execute, Query<T1, T2..>). You can get the method's signature easily from Dapper's source code.
Then, change the way of executing Dapper method in your code From: “ connection.Query(sql, param, buffer......)
”
To: “ new DapperWrapper wrapper; // Create a dapper wrapper instance wrapper.Query(connection, sql, param, buffer.....); // use this wrapper instance wherever you execute the Dapper methods and move “connection” from the beginning to your method’s 1st input argument, everything else is the same.
”
Then you can use Moq to create a Moked DapperWrapper object now and set up methods with Moq directly.