C
C#15mo ago
dave1

❔ when using client.PostAsync('...') in a test how do you go about using transactions

As title
40 Replies
JakenVeina
JakenVeina15mo ago
wat
dave1
dave115mo ago
I want to be be able to roll back database changes between tests
JakenVeina
JakenVeina15mo ago
then you need to unzip your application to the point where you can access the data context or mock the whole thing out
dave1
dave115mo ago
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
JakenVeina
JakenVeina15mo ago
building testable applications usually involves building seams into the application places where different layers or segments of the application can be ripped apart
dave1
dave115mo ago
Aye but because its crud a simple rewuest and a peer into the db should be enough
JakenVeina
JakenVeina15mo ago
right but you're trying to test at an external API, where the application is already self-contained
dave1
dave115mo ago
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
JakenVeina
JakenVeina15mo ago
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
dave1
dave115mo ago
I was crazy in my assumption Wasnt* So technically i could allow program.cs to be started with certain parameters etc. For testinf?
JakenVeina
JakenVeina15mo ago
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
dave1
dave115mo ago
When i build the app?
JakenVeina
JakenVeina15mo ago
or you want to mock/fake the whole database which is kinda a seam in and of itself yes, you absolutely could
dave1
dave115mo ago
And that wouldnt be frowned upon?
JakenVeina
JakenVeina15mo ago
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
dave1
dave115mo ago
I got a test db in a docker container right now
JakenVeina
JakenVeina15mo ago
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
dave1
dave115mo ago
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
JakenVeina
JakenVeina15mo ago
not so much, but transactions is not the mechanism for what you're describing
dave1
dave115mo ago
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
JakenVeina
JakenVeina15mo ago
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
dave1
dave115mo ago
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
JakenVeina
JakenVeina15mo ago
yes, injecting a mock would work, cause then you don't need transactions, you're not doing real data operations in the first place
dave1
dave115mo ago
Ah but it kinda takes away the test i want to write
JakenVeina
JakenVeina15mo ago
why are you insistent on writing your tests a particular way? you should be focused on what you intend to accomplish with these tests
dave1
dave115mo ago
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
JakenVeina
JakenVeina15mo ago
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
dave1
dave115mo ago
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
JakenVeina
JakenVeina15mo ago
if there's no logic, there'd be nothing to test there's obviously SOME logic
dave1
dave115mo ago
I mean no business logic, its a book of record essentially and relations
JakenVeina
JakenVeina15mo ago
sounds like either mocking EF or using the InMemory provider is the way to go
dave1
dave115mo ago
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
dave1
dave115mo ago
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<
dave1
dave115mo ago
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
JakenVeina
JakenVeina15mo ago
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
JakenVeina
JakenVeina15mo ago
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.
JakenVeina
JakenVeina15mo ago
although, they may be a bit out-of-date, the ones I'm thinkin about were written against ASP.NET Core 2
dave1
dave115mo ago
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
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.