[英]Flutter Test Mock GraphQL mutation doesn't find stub
我在我的 flutter 应用程序中使用 mockito 和 graphql 包,以便为我的 Z572CDA51902B55017C0E1A2B2 数据库编写测试而不实际执行 aFDB04B4Z 突变。 Mockito 的工作方式是我设置了一个“when”子句,这样每当在我的 mocker 上调用该特定方法时,它就会使用我定义的方法而不是正常运行的方法。
我已经为 client.mutate() 的模拟 GraphQLClient 定义了何时。 但是,当我调用 mutate 时,它说没有定义模拟。 我已经在网上进行了研究,并且有几个人通过确保他们定义了何时首先解决了这个问题,我已经这样做了,但它仍然没有解决我的问题。 这是我的初始化和包
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';
import 'package:graphql/client.dart' as graphql;
import 'login_screen_test.mocks.dart';
@GenerateMocks([graphql.GraphQLClient])
和客户和突变
final mockClient = MockGraphQLClient();
MutationOptions loginMutation = MutationOptions(
document: gql(
r'''
mutation Login($email: String!, $password: String!) {
login(input: {email:$email password:$password}) {
accessToken
refreshToken
user {
id
}
}
}
''',
),
variables: <String, dynamic>{
'email': 'doodle@noodle.com',
'password': 'password',
},
);
现在是测试用例
test('Login success if done correctly', () async {
when(mockClient.mutate(loginMutation)).thenAnswer((_) async => graphql.QueryResult(source: null, data: {'login': {'accessToken': 'ooglyboogly', 'refreshToken': 'mtnDew'}}));
await userRepo.loginEmailPassword(mockClient, email: 'doodle@noodle.com', password: 'password');
expect(store.state.auth.accessToken, isNot(''));
});
确保生成了模拟文件。 文件名应该是这样的:'your_test_file_name.mocks.dart'
如果找不到生成的模拟文件,请尝试运行
flutter pub run build_runner build
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.