Skip to content

Mocking Network Responses for tests #2833

Closed
@scottasoutherland

Description

@scottasoutherland

Question

We are in the process of trying to transition from Apollo 0.5.X to 1.0.X , and we're running into some difficulty. Specifically in 0.5.X we had a set of XCUITests which created mocked JSON responses from Apollo objects and then used that to mock our server requests during the test. Specifically, we'd create an object of type GraphQLSelectionSet, call .jsonObject on it. JSON encode that value, and then use it in a mocked network response. For instance

let myQuery = MyQuery.Data(someResponse: .init(propertyOne: "Some String", propertyTwo: 10))
let stubbedResponse = myQuery.jsonObject
stubServerResponse(stubbedResponse)

However, when trying to do the same thing in Apollo 1.0 this doesn't work for a few reasons.

  1. Mock objects don't fully turn objects into their JSON value when calling _selectionSetMockData for instance, an array of GraphQLEnums will remain an array of GraphQLEnums instead of converting each of those into their _jsonValue.
  2. Even if you convert everything recursively and serialize that, you may still run into failures because not every key is set. You could theoretically avoid this by forking the project and setting allowMissingValuesForOptionalFields but then you'd have to be very careful about how you create your mock objects to ensure that you are setting all the values you need to and if you forgot, debugging could be difficult.

So my question is, is there a way or suggestion to do this now in Apollo 1.0, i.e. to fully JSON encode either GraphQL Selection sets, or mock objects such that they can be decoded from the application as if they had come from the server directly ?

Metadata

Metadata

Assignees

Labels

featureNew addition or enhancement to existing solutionsquestionIssues that have a question which should be addressed

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions