Comments (5)
I think you need to mock the link. Take a look at how we test the underlying client https://github.com/zino-hofmann/graphql-flutter/blob/main/packages/graphql/test/graphql_client_test.dart
from graphql_codegen.
Under the hood, the generated method calls the basic query method, so it should be the same.
from graphql_codegen.
Amazing! Thanks for sharing your solution.
from graphql_codegen.
It's more for a basic query without the generated method.
For my method I need to pass a OptionsQueryGetEmailVerified?
(the option generated by the lib). I don't think I can do the same of mock the link etc...
Btw if I resolve this I will probably make an example mock test for this. Could be really useful for others.
from graphql_codegen.
I finally find the way to test it ! Thanks again @budde377 !
So if I took my example the code should be:
@override
Future<bool> checkEmailVerified() async {
final result = await graphQLClient.queryGetEmailVerified();
if (result.parsedData != null) {
return Future.value(result.parsedData?.me.emailVerified);
} else {
throw MyCustomServerException();
}
}
And the test:
group('checkEmailVerified', () {
test(
'should check if email verified and return boolean true when success',
() async {
// arrange
final tResponseData = 'get_email_verified_valid.json'.toFixture();
when(
() => link.request(any()),
).thenAnswer(
(_) => Stream.fromIterable([
Response(
data: tResponseData,
context: const Context().withEntry(
const HttpLinkResponseContext(
statusCode: 200,
headers: {'foo': 'bar'},
),
),
response: const {},
),
]),
);
// act
final result = await datasource.checkEmailVerified();
// assert
expect(result, true);
},
);
test(
'should return false when checkEmailVerified return false',
() async {
// arrange
final tResponseData = 'get_email_verified_wrong.json'.toFixture();
when(
() => link.request(any()),
).thenAnswer(
(_) => Stream.fromIterable([
Response(
data: tResponseData,
context: const Context().withEntry(
const HttpLinkResponseContext(
statusCode: 200,
headers: {'foo': 'bar'},
),
),
response: const {},
),
]),
);
// act
final result = await datasource.checkEmailVerified();
// assert
expect(result, false);
},
);
test(
'should throw MyCustomException when no coming from server',
() async {
// arrange
when(
() => link.request(any()),
).thenAnswer(
(_) => Stream.fromIterable([
Response(
context: const Context().withEntry(
const HttpLinkResponseContext(
statusCode: 200,
headers: {'foo': 'bar'},
),
),
response: const {},
),
]),
);
// act
final call = datasource.checkEmailVerified;
// assert
expect(
() => call(),
throwsA(isA<MyCustomServerException>()),
);
},
);
test(
'should throw a MyCustomServerException when a OperationException occur on graphql',
() async {
// arrange
when(
() => link.request(any()),
).thenThrow(
HttpLinkParserException(
originalException: null,
response: http.Response('', 400),
),
);
// act
final call = datasource.checkEmailVerified;
// assert
expect(
() => call(),
throwsA(isA<MyCustomServerException>()),
);
},
);
});
With get_email_verified_wrong.json
:
{
"me": {
"emailVerified": false,
"__typename": "Me"
},
"__typename": "GetEmailVerified"
}
And the get_email_verified_valid.json
:
{
"me": {
"emailVerified": true,
"__typename": "Me"
},
"__typename": "GetEmailVerified"
}
from graphql_codegen.
Related Issues (20)
- Support GQL_LINK HOT 2
- Add freezed and reactive form generator support. HOT 1
- question: Multipart file upload, scalar inside the type HOT 4
- `.graphqls` file support HOT 2
- ENUM raw values HOT 3
- graphql_codegen is incompatible with analyzer 6+ HOT 2
- Strange generation of nullable and non-nullable lists HOT 8
- code_gen takes about 4 minutes to generate with even just small change in one query HOT 5
- Problems with hasura relay api HOT 5
- Fragment doesnt seem to work HOT 3
- question: Custom type implementation HOT 3
- Codegen ignores certain Types HOT 9
- Trouble Getting Started HOT 2
- 0.13.8 breaking change HOT 2
- [Feature Request] Add support for multiple module projects HOT 13
- Add `id` in the build step like with `__typename` HOT 2
- `Invalid GraphQL: Failed to find type for field` for extended query and same name nesting fields HOT 4
- Replace `when` and `maybeWhen` with pattern matching in Dart 3 HOT 5
- [Suggestion] Generate variable names based off of aliases HOT 2
- Omit fields from a query HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from graphql_codegen.