❔ when using client.PostAsync('...') in a test how do you go about using transactions
As title
40 Replies
wat
I want to be be able to roll back database changes between tests
then you need to unzip your application to the point where you can access the data context
or mock the whole thing out
What do you mean by the first part, i thought maybe because it uses an orm there might be some magic way to do it
Okay, at least i know its not striaght forward before i go off on one
building testable applications usually involves building seams into the application
places where different layers or segments of the application can be ripped apart
Aye but because its crud a simple rewuest and a peer into the db should be enough
right
but you're trying to test at an external API, where the application is already self-contained
Yup, youre right and this is what i thought, becauee uve come from a scriptung language i figured this might be the case
And i lose that flexbility
So okay thats definitely clarified some shit
the API endpoint you're trying to test already encapsulates the entire process of connecting to the database, establishing a transaction, performing queries, committing the transaction, and closing the connection
I was crazy in my assumption
Wasnt*
So technically i could allow program.cs to be started with certain parameters etc. For testinf?
you either want seams where you can pull those operations apart, to test just the interesting part that you care about, like the "performing queries" part
When i build the app?
or you want to mock/fake the whole database
which is kinda a seam in and of itself
yes, you absolutely could
And that wouldnt be frowned upon?
one common practice for folks who REALLY want full integration testing is to setup whole test databases
and run the application on top of that
I got a test db in a docker container right now
and run tests against THAT instance of the application
I was literally about to mention docker, yes
so, you can setup your tests to just wipe part or all of the database, after confirming the results
or you can just reset the docker container
Aye this is where i was going to go with transactions and then roll back
Okay as long aas i know im not reinventing the wheel
not so much, but transactions is not the mechanism for what you're describing
Nah the transactions are what i want
So i can isolate each test from each other, its the only eay thisll work is if everything is done with ina. Transaction
Which makes me think there should be a way when building the dependencies
no, not really, if you want to do a full integration test over top of the controller, you can't use transactions, cause the controller is already doing so
if you insist that transactions is how you want to handle this, then you can't test over the whole controller
the controller is the level where transactions are already being orchestrated, you can't hijack that
Yup, i agree that it seems like its not possible
Or if it is is, it would involve injecting my own ef instance potentially in my test? Then doing some overriding of stuff in there
yes, injecting a mock would work, cause then you don't need transactions, you're not doing real data operations in the first place
Ah but it kinda takes away the test i want to write
why are you insistent on writing your tests a particular way?
you should be focused on what you intend to accomplish with these tests
I want my test to store something in the db and return a 200 response
Er my endpoint, thays what i want to test
What im thinking i dont mock engity framework, i just inject my version that will let me do ut
But id need to peer into entity framework, im kinda down this path now
So i need to get to the end
what logic within the controllers do you really want to test here?
do you really NEED to test the whole stack, or can you split it apart?
what's wrong with using a real DB and then wiping or resetting it after each test?
if mocking out EF becomes too much work here, you can consider making a seam
move your EF logic out of the controllers into a data access layer of some kind that has a simpler and more mockable API
There is no logic, its crud man
Thr app is basically a thin layer between and end point and a db
Im not mocking out a shit ton of commands, its completely pointless brittle test
Also postgis is going to be involved so its more effort to mock that, i need its functionality tested
if there's no logic, there'd be nothing to test
there's obviously SOME logic
I mean no business logic, its a book of record essentially and relations
sounds like either mocking EF or using the InMemory provider is the way to go
Inmemory wont have the featurs postgis has for spatial stuff
Aye, i may have to ininject an actual instance of ef
Or ditch it and just use linq and repositories if thays possible
Stack Overflow
asp-net core integration tests rollback transaction after each tests
I've used this documentation for my integration tests: asp.net core integration tests and this is my simplified integration test:
public class ApplicationControllerTests : IClassFixture<
Haha after all this time i found an so post trying the same thing
The thing is all this is doing is giving me a reason yk peer deeper into asp.net which as a beginner is a good thing
And you are most likely right, it might be a masssive pain in the arse, but i have to know and know why
that's fair
there is a LOT of value in peering into the depths of ASP.NET Core
Andrew Lock has some great deep dive articles about ASP.NET Core
Andrew Lock | .NET Escapades
Andrew Lock | .NET Escapades
Hi, my name is Andrew, or ‘Sock’ to most people. This blog is where I share my experiences as I journey into ASP.NET Core.
although, they may be a bit out-of-date, the ones I'm thinkin about were written against ASP.NET Core 2
Aye it seems there are so many reasons not to attempt the way i wanted to do it, so many
Testcontainers package
Seems to do everything out of the box
Seems to do everythjng... some how runs my migrations aswell
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.