C
C#•6mo ago
Krzak

Learning how to test API with NUnit and Nsubstitute

I'm trying to work out how to do that, starting with a method that fetches all of the data from the database and saves it to a list (it's async, seems like it's causing a lot of trouble). It's a really tough nut to crack, I'm reading stackoverflow, tried the documentation for NSubstitute but there isn't much there tbh. I'm trying to notice a pattern in the code fragments provided but I don't know. I wasn't able to do much on my own because of conflicting information. I'm pretty sure I got the next part wrong but: The documentation says that for the substitutions I should use an interface, not a class to avoid some weird behaviour. Ok, I can make that to substitue the database context class but the controller constructor doesn't want an interface, only the context class (I'm fully aware this might be a design mistake on my side because I'm the one that made all that lol). It seems that the substitutes need to have their behaviour specified manually but I don't understand that either. One person has told me that I don't really need to test crud if it's not complicated but apart from the API I also have a web app and that definitely needs testing (also implementing error handling) but I wanted to learn on something less intimidating first.
6 Replies
Revenger
Revenger•6mo ago
If you want to run tests against your database context, you could look into the Repository Pattern: https://learn.microsoft.com/en-us/ef/core/testing/testing-without-the-database#:%7E:text=As%20discussed%20in%20the%20testing%20overview%20page%2C%20using%20the%20in%2Dmemory%20provider%20for%20testing%20is%20strongly%20discouraged%3B%20consider%20using%20SQLite%20instead%2C%20or%20implementing%20the%20repository%20patternv Basically you can wrap your database context and use the DB in memory instead, then use it as you would normally 🙂
Testing without your Production Database System - EF Core
Techniques for testing EF Core applications without involving your production database system
Krzak
KrzakOP•6mo ago
I'll try that out, looks way more friendly. Will this be useful for testing a web app too or is something else used? The API is used by the web app so it looks like it needs a similar technique to provide needed data without asking the real database
Unknown User
Unknown User•6mo ago
Message Not Public
Sign In & Join Server To View
Krzak
KrzakOP•6mo ago
I have nooo idea what's going on :OhNo:
Revenger
Revenger•6mo ago
He basically told you what is stated on that blog i sent you, and inserted and opinion on the use of generic repo's. Not sure that you indicated that you where making such interfaces, but whatever 🙂 You can spin up a new instance of your db in memory and execute your tests as you would on a normal DB. If your list is very long you could consider using .skip and .take to page? the results, so you don't send too long a response 🙂 His point whas that you shouldn't make a generic "get" method with a predicate, and instead scope your fetches from the database to the contexts their in. For example a method that explicitly only "getsAllPeople" or whatever from the database. Does that make sense?
Revenger
Revenger•6mo ago
This explains it a bit more in detail: https://goodjava.org/blog?rec_id=17
Opinion: Stop Using the Generic Repository Pattern
Expert Articles Written by Expert Programmers. Learn about programming from our expert team of Software Engineers and Programmers.
Want results from more Discord servers?
Add your server