New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add mocked client + subclass of XCTestCase #2928
base: main
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Initial comments - it would also be good to add this to a test to a) make sure it works and b) show how to use it
@0xTim can you maybe help me, with this compiling error: In the sub class I created: XCTVaporTestCase, I have added this code on line 20: After that I got this error: That's why I added this import: But then I got this symbol error: |
@Kithin that's because you're trying to use something that links XCTest in an executable which isn't possible. If you use it in a test similar to https://github.com/vapor/vapor/blob/main/Tests/VaporTests/ApplicationTests.swift#L104 (that file is probably a good place for it) it should work |
Aaaah, so you mean, I should move this file in the test folder instead of the source folder? But is it still accessible for people to use in their own tests? |
That something is that the SpyClient I created or because that class is a subclass of XCTestCase, and this super class can only be executed in test? |
The |
I have tried it, but then I get the same error as in my previous comment:
|
I have tried it, but then I get the same error as in my previous comment, it's because the configure function is located in the executableTarget Development:
|
Can you push the current test case up? |
@0xTim I have pushed it. It's the latest commit: UPDATE - set mocked client |
hi
[image: Mailtrack]
<https://mailtrack.io?utm_source=gmail&utm_medium=signature&utm_campaign=signaturevirality11&>
Sender
notified by
Mailtrack
<https://mailtrack.io?utm_source=gmail&utm_medium=signature&utm_campaign=signaturevirality11&>
01/12/23,
12:56:50 AM
…On Thu, Jan 12, 2023 at 12:56 AM Kithin ***@***.***> wrote:
***@***.**** commented on this pull request.
------------------------------
In Tests/VaporTests/ApplicationTests.swift
<#2928 (comment)>:
> +
+ try configure(app)
Does this not mean the mocked client will not be used by the application?
—
Reply to this email directly, view it on GitHub
<#2928 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AF5PHC55Q3IEWKZIXCEEPYLWR4CMVANCNFSM6AAAAAATKUPN7Q>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
@0xTim the test keeps failing on GitHub actions, but not locally. Can you perhaps help me? |
@Kithin you're using the new generics syntax from Swift 5.7 that isn't available on older Swift versions. You'll need to find a work around |
@0xTim do you have more feedback. And I also wanted to thank you for your help, when I was stuck. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the delay - last few changes and I think we're good to go!
Co-authored-by: Tim Condon <0xTim@users.noreply.github.com>
Co-authored-by: Tim Condon <0xTim@users.noreply.github.com>
Co-authored-by: Tim Condon <0xTim@users.noreply.github.com>
@0xTim When I merge main into this feature branch, I don't get that merge conflict. But I have fixed the merge conflicts anyway. But the PR I still get the message: This branch has conflicts that must be resolved |
@Kithin It's in the application tests, I can't see the test you added anymore? |
I didn't added a new test, I updated this test: testBoilerplate |
@Kithin Hmm I see. I can't check the merge conflicts because I don't have access to the fork. So the easiest thing to probably do is close the PR, update your fork, then create a new branch and copy the code across. Let's put the test for the client in its own test case as well |
Would it make sense to add a
Then you could use it within a unit test like this:
Thanks for this by the way. |
@SpaceboatDVLP that pattern would be to store the instance of the So in your test case you'd do self.spyClient.stubResponse(httpStatus: .ok, responseData: response) That allows you to also inspect the request send to the client etc There's nothing stopping you adding the configure function in your code of course |
I have created a spy client, in order to stub the response and also being able to verify the request.
To reduce boilerplate code, I created a XCTVaporTestCase. You don't need to configure the application every time. And tearing it down. All is being handled by XCTVaporTestCase.
You just simply do this: