C
C#15mo ago
dave1

❔ how to run a test to test an endpoint and to check in the database that the data been stored

I dont know what the correct way is to do this in aspnet, or if this is how it is done in .net I want to basically post to an endpointz , say /blog Check for a 201 response then check it has been stored in the database.
19 Replies
JakenVeina
JakenVeina15mo ago
well you've just described exactly what you want so, uhhh what's the problem?
dave1
dave115mo ago
Fucking piece of shitndiscord wont let me edit my post I entered too early
JakenVeina
JakenVeina15mo ago
lol alright
dave1
dave115mo ago
Okay so i dont know the way to test an endpoint properly when iw ant to check the data has been persisted correctly Ive been looking and i dont know if im supposed to just test the controller and just bypass testing the actual endpoint Ive seen examples of just injecting a self created db context into the controller which lets me use transactions to stop tests saving to the db
JakenVeina
JakenVeina15mo ago
there's no "proper" way for any of this that's a decent approach are you looking for unit testing or integration testing? how portable do you want these tests to be?
dave1
dave115mo ago
If iw anted to test the endpoint like.. client.PostAsync(...) How do i go about testing the db?
JakenVeina
JakenVeina15mo ago
the same way you do any DB communication
dave1
dave115mo ago
So its jhst fine to use thay context Im over thinking things i think
JakenVeina
JakenVeina15mo ago
quite possibly
dave1
dave115mo ago
Ah okay cheers, ill see how far i get
JakenVeina
JakenVeina15mo ago
generally testing involves either unit testing or integration testing and there's value in both
dave1
dave115mo ago
I think the problem is i dont quite know how to load the db context in xunit Or do i just use dbcontext from my infrastructure when the app is loading
JakenVeina
JakenVeina15mo ago
not familiar with XUnit, so not sure
dave1
dave115mo ago
Ah okay
JakenVeina
JakenVeina15mo ago
but, uhh it's a DbContext make it however you like
dave1
dave115mo ago
I dont know how becauee it has its own set of dependencies ,i think ineed ot leanr how the application builder works (Im a dev from another language) Im on day 3 of .netting, quite enjoying it so far, cheers for your guidance I realised i can use the builder anywhere i think
JakenVeina
JakenVeina15mo ago
you can construct DbContext objects directly, as long as you can pass in options you can also just create a skeleton IoC container if you wanna reuse DI setup code if you're only on day 3 of using .NET you MIIIIIIIGHT be jumping the gun into all of these high-end frameworks
dave1
dave115mo ago
Maybe, but i got another week, once i can write the first test and understand whats ill be flying
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.