Unit Testing ASP.NET async api service
Okay. I am trying to make a unit test for my Customer Service. I tried starting with the Read operation which looks like this in my service:
I tried using this as a guide: https://learn.microsoft.com/en-us/ef/ef6/fundamentals/testing/mocking#testing-query-scenarios
Testing with a mocking framework - EF6
Testing with a mocking framework in Entity Framework 6
17 Replies
I tried implementing it myself like this:
But when I run
dotnet test
it tells me: The provider for the source 'IQueryable' doesn't implement 'IAsyncQueryProvider'. Only providers that implement 'IAsyncQueryProvider' can be used for Entity Framework asynchronous operations.
.
And because this is me writing my first test I have absolutely no clue what this means. And I don't really know where to start to debug it.
I tried adding the "TestDbAsyncQueryProvider" code they provided but it changed absolutely nothing in the error so it didn't help me to fix it.
$pasteIf your code is too long, you can post to https://paste.mod.gg/, save, and copy the link into chat for others to see your shared code!
Full error: https://paste.mod.gg/byjidngksegg/0
BlazeBin - byjidngksegg
A tool for sharing your source code with the world!
I was able to test it if I made my service (and the test) synchronous, the error only came when I tried to make it async.
If one looks at this code and thinks that it is quite the mess, I would appreciate perhaps good videos or websites on how to do this correctly.
I'm on my phone and can't comment on your mocking setup, but as a general guidance I'd probably not test ef core logic since it's not something maintained by yourself.
If you have complex enough queries to warrant a test, I'd always favor an integration test with test containers of the database you'll actually run with in the end
Since you get to test the sql translation which will actually take place later on
If you mock things or use an in memory ef core db, then you're kinda ripping out 80% of the code and your test has little representation of what's going on
What are integration tests used for?
Because (for now) it is only an API. Not connected to any other program so it's not "integrated" with anything right?
If you use ef, you'd probably be integrating with a database
It qualifies as another system you integrate with
Ooo okay
Would you happen to have any links that show how to do proper integration testing?
There's a good msdn article for integration tests with asp net core https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-8.0.
This might get you the general idea 👍 If you ask me, I'd definitely recommend reading on testcontainers as well if you're doing integration testing in any form, but I feel like you're just getting started with testing, so taking it step by step is a good idea 👍
Integration tests in ASP.NET Core
Learn how integration tests ensure that an app's components function correctly at the infrastructure level, including the database, file system, and network.
Thank you
@Sossenbinder Do you think I should drop the unit tests than?
then*
Or perhaps use them to test something differently?
This wouldn't be something I'd unit test, no. I mainly unit test complex logic and try to keep the dependencies to external systems pretty clean within, so I don't have to rely on mocking and can just pass input and output.
When it comes to database interaction which warrants testing, I usually reach out for integration tests
Is there anything I should unit test in my API?
Don't know what would be complex logic. I feel like my API is pretty simple
just CRUD mostly
That depends on your code I assume. If it's a pretty basic crud api without much logic, it might not be something which contains a lot of unit testable code
https://enterprisecraftsmanship.com/posts/when-to-mock/ Here's another great link. Generally recommend vladimir khorikov for testing theory, his book is great
Enterprise Craftsmanship
When to Mock
The use of mocks in unit testing is a controversial topic (maybe less so now than several years ago). I remember how, throughout my programming career, I went from mocking almost every dependency, to the "no-mocks" policy, and then to "only mock external dependencies".
None of this practices are good enough. In this article, I’ll show you whi...
Okayyy
I will try to use this, thanks a lot