Guidance for unit testing when FromSql<> is used.
See original GitHub issueWe have unit tests that use the InMemory
provider.
However, a few of our objects under test are calling the FromSql()
method to materialise objects from stroed procedures.
When using the InMemoryProvider this obviously doesn’t work.
One solution i can think of is for the object under test to use the Repository pattern - so we can swap EF7 out entirely at test time, for an InMemory repository. However I think it would be good if the InMemory provider had a way to handle FromSql at test time, so you could map an abitrary set of results when needed?
Issue Analytics
- State:
- Created 7 years ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Guidance for unit testing when FromSql<> is used. #7212
We have unit tests that use the InMemory provider. However, a few of our objects under test are calling the FromSql() method to...
Read more >Entity Framework Core FromSql mock test cases
Don't mock FromSql and DbContext in general. Write and execute tests against actual database. Mocking will provide zero value and waste the time ......
Read more >Testing against your Production Database System - EF Core
Note that testing against a different database than what is used in production (e.g. Sqlite) is not covered here, since the different database ......
Read more >Integration Testing with Entity Framework Core and SQL ...
Entity Framework Core makes it easy to write tests that execute against an in-memory store. Using an in-memory store is convenient since we ......
Read more >SQL Server Unit Testing in Visual Studio
Visual Studio with SQL Server Data Tools can be used to perform unit testing against stored procedures to test expected output and behavior....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I don’t think this really solves the issue though… we use InMemoryProvider during testing, because we want to eliminate the need for having an external database asset to manage. This means we can run unit tests on any machine quite easily, with minimal setup.
Using sqllite provider at test time isn’t an option if the subject under test is calling FromSql<> and passing in a variant of sql like for example, Postgres. (Because you use Postgres in production) or executing a sproc.
So the only reliable way to get this to work is to not use the InMemory provider, and use the full blown relational provider - which brings us back to square one - managing the external database at test time.
If there was some mechanism to mock or map FromSql<> and still use the InMemory provider I think that is what we would like to do.
Thanks for responding. I deleted my comment because I realized it was a stupid question. I did just what you suggested as soon as I deleted the comment.
Thanks again!