C
C#2mo ago
Cutterka

Unit testing

Hi i have a question, can somebody help me out i dont uderstand why i get the error that Zdravnik doesnt exist if when debugging i can clearly see its passed correctly
No description
No description
No description
No description
5 Replies
Becquerel
Becquerel2mo ago
have you created the table in your actual sqlite instance? EF Core knowing the table exists in your model doesn't mean it exists in the physical database consider using the methods context.Database.EnsureCreated() and/or context.Database.Migrate()
Cutterka
Cutterka2mo ago
what do you mean exactly by exist in the physical database ? my database exist for sure because i can see it in DbBrowser, but context.Database isnt only used when creating a InMemo database? okay thank you so much i added context.Database.EnsureCreated() and now it finally works
Becquerel
Becquerel2mo ago
you aren't using an in memory database i believe
Cutterka
Cutterka2mo ago
no i am not, so if i just build a new ApplicationDbContext as before and pass the options, it doesnt automatically recognise the already created database? I have another question, i get the same error as before when triying to pass an entier ZdravnikViewModel to my test method, could it be related to the same problem as before?
Becquerel
Becquerel2mo ago
if you already have a physical database there, then EF Core should recognise it exists when you make a DbContext one issue with having real databases like these in your unit tests is that they inherently make your tests non-repeatable by default because you now need to make sure the database is created at the start and dropped at the end because otherwise you have data from last time there when you run your test if you are just doing this as a learning exercise it's fine of course but for something more serious i would suggest you look into Testcontainers as a way to easily and safely spin up databases for testing