作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在尝试根据 Weather tutorial 测试我的 API 提供程序由 Felangel 编写,因为它与我的代码非常相似,但我无法使其工作。
api_provider_test.dart
import 'package:stock_mobile/data/models/user.dart';
import 'package:test/test.dart';
import 'package:mocktail/mocktail.dart';
import 'package:http/http.dart' as http;
import 'package:stock_mobile/data/providers/api_provider.dart';
class MockHttpClient extends Mock implements http.Client {}
class MockResponse extends Mock implements http.Response {}
class FakeUri extends Fake implements Uri {}
void main() {
group('ApiProvider', () {
late http.Client httpClient;
late ApiProvider apiProvider;
setUpAll(() {
registerFallbackValue(FakeUri());
});
setUp(() {
httpClient = MockHttpClient();
apiProvider = ApiProvider(httpClient: httpClient);
});
group('login', () {
test('Throws ApiException non-200 response', () async {
final response = MockResponse();
when(() => response.statusCode).thenReturn(400);
when(() => response.body).thenReturn('');
when(() => httpClient.post(any())).thenAnswer((_) async => response);
final actual = await apiProvider.login(const User.empty());
expect(actual, throwsException);
});
});
});
}
api_provider.dart
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:stock_mobile/data/models/user.dart';
class ApiProvider {
ApiProvider({http.Client? httpClient})
: _httpClient = httpClient ?? http.Client();
static const _baseUrl = '192.168.0.5:3000';
final http.Client _httpClient;
Future<String> login(User user) async {
final loginRequest = Uri.http(_baseUrl, '/login');
final loginResponse = await _httpClient.post(loginRequest, body: {
"username": user.username,
"password": user.password,
});
if (loginResponse.statusCode != 200) {
throw Exception();
}
return jsonDecode(loginResponse.body);
}
}
user.dart
import 'package:equatable/equatable.dart';
class User extends Equatable {
const User(this.username, this.password);
final String username;
final String password;
const User.empty({
this.username = '',
this.password = '',
});
@override
String toString() {
return 'User{username: $username, password: $password}';
}
@override
List<Object?> get props => [username, password];
}
当我尝试运行代码时,出现了这个错误:
package:http/src/client.dart 62:20 MockHttpClient.post
package:stock_mobile/data/providers/api_provider.dart 17:45 ApiProvider.login
test\unit\api_provider_test.dart 42:42 main.<fn>.<fn>.<fn>
test\unit\api_provider_test.dart 35:52 main.<fn>.<fn>.<fn>
type 'Null' is not a subtype of type 'Future<Response>'
然后,我尝试通过添加 when(() => apiProvider.login(const User.empty())).thenAnswer((_) async => response.body);
来修复它> 在 api_provider_test.dart 中测试,因为 FAQ here关于这个错误,但我无法让它工作。
这是错误:
package:http/src/client.dart 62:20 MockHttpClient.post
package:stock_mobile/data/providers/api_provider.dart 17:45 ApiProvider.login
test\unit\api_provider_test.dart 34:32 main.<fn>.<fn>.<fn>.<fn>
package:mocktail/src/mocktail.dart 211:8 when.<fn>
test\unit\api_provider_test.dart 34:13 main.<fn>.<fn>.<fn>
test\unit\api_provider_test.dart 28:52 main.<fn>.<fn>.<fn>
===== asynchronous gap ===========================
dart:async _completeOnAsyncError
package:stock_mobile/data/providers/api_provider.dart ApiProvider.login
test\unit\api_provider_test.dart 34:32 main.<fn>.<fn>.<fn>.<fn>
package:mocktail/src/mocktail.dart 211:8 when.<fn>
test\unit\api_provider_test.dart 34:13 main.<fn>.<fn>.<fn>
test\unit\api_provider_test.dart 28:52 main.<fn>.<fn>.<fn>
type 'Future<String>' is not a subtype of type 'Future<Response>'
package:http/src/client.dart 62:20 MockHttpClient.post
package:stock_mobile/data/providers/api_provider.dart 17:45 ApiProvider.login
test\unit\api_provider_test.dart 36:42 main.<fn>.<fn>.<fn>
test\unit\api_provider_test.dart 28:52 main.<fn>.<fn>.<fn>
type 'Null' is not a subtype of type 'Future<Response>'
提前致谢...
最佳答案
我认为您在模拟 HttpClient 方法时缺少命名参数。
替换when(() => httpClient.post(any())).thenAnswer((_) async => response);
与
when(() => httpClient.post(any(), body: any(named: "body")))
.thenAnswer((_) async => response);
这现在应该正确匹配您的方法调用。
关于flutter - 无酒精鸡尾酒 : type 'Null' is not a subtype of type 'Future<Response>' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69889524/
我是一名优秀的程序员,十分优秀!