C
C#4d ago
Mango

How do I write integration tests

Here is my scenario: User interacts with UI (Avalonia) -> user inputs information, lets say its checkboxes in a checklist -> user clicks I.E submit button -> command on view model takes all the data the user inputted on the UI and sends it to an api parsing service -> api parsing service formats all the data into the expected API objects and sends it to the writing service -> writing service sends it off to the api. What I want to automatically validate is that the API response in every command is successful. Our services are broken out into interfaces like IApiParsingClient, IApiWriteClient, and I don't have an issue hard coding test data somewhere to do this. I need to learn how and where to start
79 Replies
Teddy
Teddy3d ago
change the title to integration tests :p
Mango
MangoOP3d ago
How do I change the title :kekw:
Pope
Pope3d ago
open a #help thread
Mango
MangoOP3d ago
this is one silly
Pope
Pope3d ago
I don't get what you're trying to test here. viewmodel or API?
Teddy
Teddy3d ago
if there's no test project yet, create a new XUnit project to start
Mango
MangoOP3d ago
@Teddy So yeah what I wanna write tests for is simulating, which I can hardcode, user data from the UI to the relay commands that process The commands I wanna focus on right now interact with a client class that talks to our API I want to expect successful results back from the API I would like to start at VM level which calls the api client bc some of our VM code modifies data to fit the api expectations But if that gets messy I can go straight to api client testing to start out with Okay I have created a new xunit project under my solution
Teddy
Teddy3d ago
Ainz
Ainz3d ago
you know we have xunit projects already right
Mango
MangoOP3d ago
No
Ainz
Ainz3d ago
you do now
Teddy
Teddy3d ago
😂 that was one of the first things I asked
Mango
MangoOP3d ago
Where
Ainz
Ainz3d ago
in the tests solution folder
Mango
MangoOP3d ago
They dont have tests in them if we do Oh thats been hidden for months since we dont interact with them lol
Mango
MangoOP3d ago
Insider information :kappa:
No description
Ainz
Ainz3d ago
There's a project for API, services, and UI tests I never said I had tests I said test projects lol
Mango
MangoOP3d ago
We don't spin the API up so I can skip that API is 24/7 by India
Teddy
Teddy3d ago
it'll run a lot faster if you run it locally
Mango
MangoOP3d ago
I made tests and all are passing.
Assert.True(true)
Assert.True(true)
Production ready My hands r tied
Pope
Pope3d ago
never have a unit test reference a non-local API 😄
Mango
MangoOP3d ago
By Pune, India
Teddy
Teddy3d ago
i mean you could, it'll just take forever and debugging will be a nightmare
Pope
Pope3d ago
I want to have 1k unit tests that run in a second
Mango
MangoOP3d ago
We'll have to do that
Pope
Pope3d ago
adding an API call to that is fucking hell
Mango
MangoOP3d ago
I don't really think we have a choice, right @Ainz ?
Ainz
Ainz3d ago
the database is kind of the problem here I have a mirror of the API source code, but without a local database and all of the stored procedures that go with it, the API is kind of useless All of the business logic is stored procedures
Mango
MangoOP3d ago
We will just accept slow testing We r used to slow
Ainz
Ainz3d ago
The API acts more as a gateway / forwarder to a SP call at the DB
Pope
Pope3d ago
tsqlt testing, then
Kao
Kao3d ago
The real question is: what are you testing with your tests if the api itself is useless :HUHH:
Teddy
Teddy3d ago
I guess it's more of a 3rd party regression testing
Kao
Kao3d ago
Anyway, one day a wise man said: testing is doubting.
Mango
MangoOP3d ago
We are different teams They push updates independently of us We want to automatically test all of our API calls every time they update To make sure we are still in sync with them
Teddy
Teddy3d ago
anyway @Mango you can start by adding a reference to the client/UI in your xunit project, then make use of its client to make API calls and assert on the responses
Mango
MangoOP3d ago
Sometimes they change fields without telling us Then everything is broken and its impossible to debug bc they don't give back normal error messages And its a huge PITA to figure out Hours of guesswork :sadge: We'd like to catch it as soon as possible
Pope
Pope3d ago
Learn to coordinate. Head up that initiative.
Teddy
Teddy3d ago
yeah, so really you're regression testing someone else's API
Pope
Pope3d ago
Get promoted over A
Teddy
Teddy3d ago
but really they should be the ones doing it
Pope
Pope3d ago
yeah. you give them something they can run against first.
Mango
MangoOP3d ago
I don't think @Ainz would mind one bit if I got promoted to a position to support them In fact he'd probably laugh at me and say nope dont even bother me with it Nobody wants that responsibility
Pope
Pope3d ago
other than this
Surprise, we shipped!
bullshit
Mango
MangoOP3d ago
We have plans pushing them to improve things surely but slowly The guys we have on it right now are good people. Just needed direction which Ainz is setting them up with I'm hopeful for the future anyways
MODiX
MODiX3d ago
Teddy
anyway <@386969677143736330> you can start by adding a reference to the client/UI in your xunit project, then make use of its client to make API calls and assert on the responses
Quoted by
React with ❌ to remove this embed.
Teddy
Teddy3d ago
not that I think it's the best approach, but it's somewhere to start make sure the API you're calling isn't connected to a prod DB
Mango
MangoOP3d ago
Why not best approach? Heh its connected to prod db but we have an account that is for test results on the prod db
Teddy
Teddy3d ago
because like me and @Pope said, you're adding a lot of latency to the tests, so if you want to automate them (include in your CI/CD) it'll slow things down substantially, you also can't just set breakpoints in the API to help debug since it's running somewhere remotely
Mango
MangoOP3d ago
Yeah it kinda sucks But it is what it is
Pope
Pope3d ago
Be the improvement we want to see. Believe in yourself.
Mango
MangoOP3d ago
Rewrite all the SP from scratch and host my own database I'll have it done by tonight thanks Pope I feel inspired
Teddy
Teddy3d ago
this is a good example of outsourcing gone too far though and the negative consequences from it
Pope
Pope3d ago
I still think it could be made to work
Teddy
Teddy3d ago
they need to take some ownership of the code back from the offshore
Pope
Pope3d ago
just need a bit of collaboration on expected changes. Communicate what changes are being made, have them write a test harness or something i see it being pretty painful, though. nm. it's just going to turn into waterfail development
Mango
MangoOP3d ago
Timezones are the biggest issue We have an actual corporate in India so they aren't "offshore" in traditional sense. They have their own contracted normal hours to work They are full employees not an offshore contract So we cannot force them to be on our timezone and vice versa
Pope
Pope3d ago
then more lockstep dev
Mango
MangoOP3d ago
@Zane so I reference my api project This means I have to re-implement my IWriteClient in the test project right? No way to bring my implementation over from the other project? That would be pretty magical https://nsubstitute.github.io/
Teddy
Teddy3d ago
ah, yes, this answers the previous question I somehow missed the ping on you can bring in your implementations whenever applicable though
Mango
MangoOP3d ago
@Zaneris This shit is so involved But I think I understand now
Teddy
Teddy3d ago
it can be yeah, depending on how complex your service chain is
Mango
MangoOP3d ago
I thought testing was plug and play not gonna lie Complex AF I have another problem We have a method with an out parameter. I need that method mocked The return is useless to me I somehow need to define that out in nsubstitute not the return Can I do that Aha got it Thats actually nice syntax
Teddy
Teddy3d ago
see, you're a pro already
Mango
MangoOP3d ago
A huge part of it is that its just so boring And tedious
Teddy
Teddy3d ago
this is why tests often get delegated to the interns/juniors but then they need help and the senior ends up doing all of the work anyway 😄
Mango
MangoOP3d ago
Me in a call with @Ainz rn LMAO But he is busy so we r just rubber ducky rn And chatting
Ainz
Ainz3d ago
he needs better taste in music it comes through his mic
Mango
MangoOP3d ago
apple music recommendations are garbage So I have, on phone so ima shorthand stuff
public ClientFixture()

public IMyUsableClient get set
private IClientFactory get set

Ctor()
{
CreateDependencies()
}

CreateDependencies()
{
CreateClientFactory()
}

CreateClientFactory()
{
_factory = Substitute.For<IClientMusic>();
_factory.CreateWriteOnlyClient(out Arg.Any<IWriteOnlyClient>)
.Returns(x => {
var httpClient = new() { stuff }
x[0] = new WriteOnlyClient(httpClient)
return Arg.Any<ExpectedReturn>
});
public ClientFixture()

public IMyUsableClient get set
private IClientFactory get set

Ctor()
{
CreateDependencies()
}

CreateDependencies()
{
CreateClientFactory()
}

CreateClientFactory()
{
_factory = Substitute.For<IClientMusic>();
_factory.CreateWriteOnlyClient(out Arg.Any<IWriteOnlyClient>)
.Returns(x => {
var httpClient = new() { stuff }
x[0] = new WriteOnlyClient(httpClient)
return Arg.Any<ExpectedReturn>
});
Gotta do this like 3x more to get to the usable client Or get up to it I should say
Pope
Pope3d ago
I believe you can get it up 🎉
Teddy
Teddy3d ago
at least once it's done you can just write actual tests from there forward
Mango
MangoOP3d ago
So true Question So when I go to make my usable client which requires all these deps I have to make an actual instance of it right I can't use substitute and somehow insert these other mocks Internet looks like its telling me I can make an actual instance now but just sanity checking
Teddy
Teddy3d ago
2 options, register the mocked services through DI and then the new client will just grab the mocked ones automatically, or just instantiate the client with you providing the mocked versions yourself I usually just instantiate it myself providing all of the mocks
Mango
MangoOP3d ago
Doing ILogger is gonna be weird
Pope
Pope3d ago
Why do you need to? Just assume it's perfect and ignore it.
Mango
MangoOP3d ago
My client requires it and tries to use it Do I not have to mock it? Im currently mocking factory 2/10 wooo Which requires me to setup like another 5 dependencies
Pope
Pope3d ago
Pass in an ILogger that does nothing.
Sehra
Sehra3d ago
NullLogger<T>

Did you find this page helpful?