Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

One Approach to Moq dapper methods that not supported by this project #85

Open
Z-Zhao opened this issue Jan 25, 2023 · 0 comments
Open

Comments

@Z-Zhao
Copy link

Z-Zhao commented Jan 25, 2023

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.

@Z-Zhao Z-Zhao changed the title Approach to Moq dapper methods that not supported by this project One Approach to Moq dapper methods that not supported by this project Jan 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant