Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

66%
+2 −0
Q&A What are the cons of directly mocking Entity Framework DbSets instead of working with an in-memory database when unit testing the application?

I have recently contributed to a Clean Code project and had a discussion about how to implement unit tests. The project author argues for using an in-memory database (which easily replaces the rea...

1 answer  ·  posted 4y ago by Alexei‭  ·  last activity 4y ago by Goyo‭

#1: Initial revision by user avatar Alexei‭ · 2020-11-29T11:50:07Z (almost 4 years ago)
What are the cons of directly mocking Entity Framework DbSets instead of working with an in-memory database when unit testing the application?
I have recently contributed to a Clean Code project and had a discussion about [how to implement unit tests](https://github.com/jasontaylordev/CleanArchitecture/pull/251).

The project author argues for using an in-memory database (which easily replaces the real one) instead of mocking the DbSets and now I am doubting my own approach. 

The in-memory database approach for unit testing means that in order to unit test, the project setup is changed to use an in-memory provider instead of a real one (e.g. SQL Server provider). All services dependencies remain unchanged (no need for mocking here).

The DbSets mocking means that for each service I need to explicitly mock the dependency (DbSet<SomeType> is mocked using a library to a static list).

I tend to favor my approach because:

- sounds purely unit testing: mock the inputs, not an indirect input as the database
- no need for a special set up for testing (i.e. unit tests do not have to know anything about the infrastructure)

I am wondering what cons the DbSet mocking might have.