-
Notifications
You must be signed in to change notification settings - Fork 731
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
How to mock responses for UI tests? #188
Comments
Once #186 is in, you could just add a terminating link returning fake responses. Right now, I think the caching system could be used. @martijnwalraven was gonna add a function for injecting results straight into the cache, don’t think that’s in yet though. |
Using the built-in Apollo cache. I don't know if you could pre-warm the cache by recording a run and just copying the sqlite cache db into the test bundle. Otherwise, you could inject results runtime once Martijn has exposed the function needed. |
You could also initialize the store with records before each test, that is how the Apollo iOS tests work. Something like: let store = ApolloStore(cache: InMemoryNormalizedCache(records: [
"QUERY_ROOT": ["hero": Reference(key: "hero")],
"hero": ["__typename": "Droid", "name": "R2-D2"]
])
)
let client = ApolloClient(networkTransport: networkTransport, store: store) |
Hello guys, I have issues mocking my models too. Any update in Apollo about this topic? Btw, I didn't understand the answer posted above. If you @martijnwalraven could extend your explaining would be great!, thanks! |
Yes I definitely want to get us handling this case better. |
When we investigated GraphQL as a solution and alternative to a Restful API to be consumed by native and web clients we mistakenly assumed mocking was out of the box due to this article https://www.apollographql.com/docs/graphql-tools/mocking/ and others in Google. Only when we went all in did we realise that only the web folks are able to take advantage of...
Well, only if you're using JavaScript it seems. We've been in a world of pain trying to mock and stub GraphQL for our UI feature tests where the sut is a black box. Unit Tests not too bad, we just stub at the network layer but UI tests have been hell. We're using an HTTP transport but as GraphQL is not Restful the URL of the requests is not unique so we can' t just set up a small local web-server and just return stubbed responses. We thought about parsing the incoming request body to a local server but then we would need to be able to parse GraphQL syntax to identify the type of request in order to return the correct mocked response and that wouldn't even allow for variations i.e. testing error scenarios etc. The other issue even if we did something like this is keep the mock responses up-to-date with the schema. If we for example kept a bunch of JSON file every time the schema changed we would have to go through a world of pain again while all our tests fail and we would need to generate new JSON stubs and variations. For me this is the number one issue that is blocking us from really adopting GraphQL in a commercial scope where automation tests are a mandatory requirement to ensure product quality. It's disappointing that questions like this have been asked since 2017 and very little movement has been made. If anyone has any ideas or solutions in order to mock GraphQL for UI tests in a robust a future proof way I would really appreciate it. |
Two issues with this:
|
One thing I'm going to be looking at after we get 0.13 out the door is potentially making it possible to pass in a Easier mocking is a lower-down reason on the list to do this, but it does seem like it'd be a great side benefit. |
We already do this, we've got our own networking framework which has stubing built-in and we've got our own custom But it still has the following disadvantages:
It would be nice if there was a similar solution to
|
Personally I'd call anything where you're mocking out the API layer gray-box testing rather than black-box testing, but that's probably more of a philosophical argument than a relevant point here. In terms of purely black-box testing, especially if you're using XCUI, you're right, this is a total pain in the ass because XCUI tests run out of process and changes have to be made within the shipping app to accommodate it (which drives me up a wall and I have been whining about it on Radar since XCUI was introduced). This is a huge reason why a
In my personal experience this has always been an issue if I'm returning responses from a file, even in REST APIs. When something changes on the backend, you have to account for it in your mocks, or they won't work. Overall, I agree that this situation is not ideal. The solution I've used in the past is to use Four years later, I'm still cranky with Apple for not allowing gray-box with XCUI, exactly because of issues like this one. Wish I had a better answer for you. |
Yeah Apple have never seemed that invested in testing in general. I kind of understand the reasons for the out-of-process approach |
It basically comes from XCUI being adapted from a black-box JS testing framework they used to use for QA, which to me is a wholly different use case than developer testing. I've probably complained about it enough in this thread already though 😛 |
Two new options for this have shipped with
If you have further requests around this or have problems with either of the new methods, please open a new issue. Thank you! |
@martijnwalraven Hey there! How are you defining
Thank you! Love Apollo. :) |
@noisypigeon - I believe this test demonstrates how we're mocking network fetches. |
@calvincestari Thank you! 🙏🏻 |
Since we have ApolloStore with preconfigured RecordSet why do we have to mock the network fetches as well ? What am I missing here - if I add my mock data to the ApolloStore as shown above do I have to also mock the network fetches ? It feels like having the ApolloStore mock should be sufficient - if that is the case how could we add an implementation of the NetworkTransport that doesn't do anything ? Thanks |
General question about how I could go about writing UI tests that don't actually hit the network? I'd love to create some basic UI tests for GitHawk, but I'm not sure how I can inject some local JSON responses into Apollo so the tests are consistent and don't hit the network.
Any pointers to what I could do?
The text was updated successfully, but these errors were encountered: