[XUnit, Moq, EFCore] Unit Testing Question
Hi folks, needing some advice on this approach. This is my first real go at unit testing, so wanted to make sure I'm off in the right direction.
My app works as so: Frontend calls API, API calls service class, service class has direct access to dbContext. Simples.
So here's an example of one of my service classes and a method, and the unit test:
Service class:
Test class:
So, am I doing anything wrong?
6 Replies
Have just realised that I need to amend my
.UseInMemoryDatabase
as it just gets reused on each test. So I've passed in Guid.NewGuid().ToString()
as the name insteadNothing stands out as wrong, but I would question the value of creating unit tests for simple CRUD operations like this - that's more the domain of integration tests in my opinion
The service methods will eventually incorporate business logic and mapping data model > dto, so they will become more complicated
or well "complicated"
I would question mixing business logic, mapping and CRUD under the banner of 'service'
those are all separate concerns
personally I think unit tests are most valuable for business logic because they provide documentation, verification and scope all at once
whereas testing 'does entity framework give me back the data i've just put into it' is less useful
So would you recommend I split my layer into a DAL and BLL?
you should still have that tested somewhere, but as an integration test
i would recommend not worrying about acronyms until you need to 🙂
but yes
i think business logic should be as separate from everything else as is reasonable