这是我要测试的服务。
registerSchemesAsPrivileged
这是我的考验:
class AccountService {
AuthLibrary _library;
BehaviorSubject<User> _user;
Stream<User> get user => _user.stream.asBroadcastStream();
AccountService(this._library) : _user = BehaviorSubject() {
checkUserLogin();
}
Future<void> checkUserLogin() async {
final token = await _library.getToken();
final tokenLength = token?.length ?? 0;
return _user.add(tokenLength > 0 ? User(token: token) : null);
}
}
我在互联网上找不到如何通过此测试。我发现那里的每个示例都在使用相同的模式,但是由于某种原因,它在这里失败了。 因此,当我运行测试时,出现以下错误:
class MockAuth extends Mock implements AuthLibrary {}
main() {
AuthLibrary _auth0;
setUpAll(() {
_auth0 = MockAuth();
});
test('set user if token is not empty', () async {
final expected = User(token: "21398712938721");
when(_auth0.getToken()).thenAnswer((_) async => expected.toString());
final service = AccountService(_auth0);
verify(_auth0.getToken()).called(1);
expect(service.user, emits(expected));
});
}