✅ xUnit | Repository not saving to database
Hello, so i started writing tests for my web api. Issue is that for whatever reason when i call the route, in test, with correct data, it doesnt seem to be saving it. It does save when I call the route normally via SwaggerUi. I tried mocking the save method in repositoey but no luck with that. Any help would be appricieted, thanks in advance.
(Will post rest of related code in next message)
CustomWebAppFactory
35 Replies
Controller & Service & Repository
And finally Test
Unknown User•15mo ago
Message Not Public
Sign In & Join Server To View
sqlite would be easy to set up and test with
Used SQLite as well, same result
But will check on proper database
sqlite is proper enough
Unknown User•15mo ago
Message Not Public
Sign In & Join Server To View
so uhm
you mock the context in your test
Unknown User•15mo ago
Message Not Public
Sign In & Join Server To View
oh, but you never seem to call or use that mock. Okay.
uh wait i might've copied old code
Ok no, it's the proper one, also with proper database it's same thing
and if you debug the test with breakpoints in your controller, it hits?
just to be sure that it invokes correctly and passes the middleware
It hits the controller and the service
great
But when in repository i return what is passed down to the function, it doesnt return it
It's shown in the code i sent
this part? if you put a breakpoint here, does
paymentData
look reasonable before and after the savechanges?Will check, give me second
Interesting, it seems like it doesnt even want to go inside of the function, whei put breakpoint on
_dataContext.SaveChanges();
the test continued, but when i put breakpoint on var result = _paymentDataRepository.Add(paymentData);
in service it stopped normally
🤔
Oh i forgot to add this as well:
This is inside CustomWebAppFactory
May this change anything?If its actually used anywhere, yes
but you seem to be mixing unit testing with mocks and WAF testing
I would not expect any mocks while using WAF
I might be 😅 It's first time when i write WebApi in ASP.Net, same with testing in c#
For curiosity sake, what's WAF?
WepApplicationFactory
Oh oke
its what you use to "spin up" a backend in memory
and honestly this is great end to end testing of your backend
Okey, so i deleted the last two lines i sent and it's actually working with proper database
the only way to make it better is to use something like TestContainers to run against a real database
Will try now with sqlite
cool
Damn it works
Thanks a lot
right, so dont mock the parts you want to actually test 😄
Will keep it mind
👍
If you are done with this thread, feel free to $close it
Use the /close command to mark a forum thread as answered
I guess having someone to explain the problem is helpful after all :D
you did good thou
you asked in a proper way and provided more information when asked, and fast too
Aint the first time having some problems
you get 10/10 in asking for help
And you did 10/10 helping me
Till next time :p