gpt4 book ai didi

flutter - 无酒精鸡尾酒 : type 'Null' is not a subtype of type 'Future'

转载 作者:行者123 更新时间:2023-12-05 01:56:21 26 4
gpt4 key购买 nike

我一直在尝试根据 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com