Unit Testing with NUnit
Hello everyone. I got this service that is called by a controller and gets data in the Db.
Does this unit test, test anything? or because everything is Mocked it actually tests nothing?
Because the method GetAllUsersAsync appears green as if it was tested. But if I Debug the test it never goes into the function in the code. I guess that's because everything is mocked. Not sure. Some help here. Thank you
1 Reply
This doesn't really test anything.
Consider that with unit tests you want to verify your applications logic, to see that what you think happens is what actually happens.
Now there isn't much logic to this piece of code. All it does is return an IEnumerable, that's it.
If you want to perform an actual test of the database, that's a gnarly topic altogether, and regardless of the route chosen to perform such a task, this is not it (you'd be looking at either testing the actual database or an in-memory database created with the exact same script).
So you mentioned a setup where a Controller receives a request to fetch something from a service.
This service then calls deeper down the stack to the database to return something.
Here's some examples that would require unit tests (these aren't really applicable to your current situation, but you can see from these what I mean by logic):
Case: An unauthenticated user requests all users.
Test: Assert that the response is as expected (unauthenticated, bad request etc)
Case: An authenticated user requests all users.
Test: Assert that the response is 200 ok with content.
Case: A user requests something nonsensical.
Test: Assert that your request validation does as it should.
As you can see from these examples, they contain logic that is absent from your original example. With that it should become clear when we create unit tests and when we dont. 🙂