我的Flutter应用程序中有一个存储库类,使用以下方法返回Stream:
Stream<List<Product>> getProducts() async* {
var currentUser = await this._auth.currentUser();
if (currentUser == null) {
throw AuthException('not_logged_in',
'No current user found probably because user is not logged in.');
}
yield* ...
}
按照SO上的这个答案,从异步生成器函数引发异常的上述方法看起来不错。
如何编写测试(带有测试包),以便测试此方法引发的异常?
像这样的东西不起作用:
test('should throw exception when user is not logged in', () {
final _authSignedOut = MockFirebaseAuth(signedIn: false);
final _repoWihoutUser = FirebaseProductRepository(
storeInstance: _store,
authInstance: _authSignedOut,
);
var products = _repoWihoutUser.getProducts();
expect(products, emitsError(AuthException));
});
也没有:
expect(callback, emitsError(throwsA(predicate((e) => e is AuthException))));
甚至没有:
var callback = () {
_repoWihoutUser.getProducts();
};
expect(callback, emitsError(throwsA(predicate((e) => e is AuthException))));
你近了 您的第一次尝试:
expect(products, emitsError(AuthException));
不起作用,因为emitsError
将aMatcher
作为参数,因此您不能直接将其传递给类型。相反,您需要使用:isA<T>()
Matcher
expect(products, emitsError(isA<AuthException>()));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句