내 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에, 상기 방법은 비동기 함수 발생기 보이는 미세에서 예외를 발생한다.
이 메서드에서 throw 된 예외를 테스트하기 위해 테스트 ( 테스트 패키지 포함)를 어떻게 작성 합니까?
이와 같은 것이 작동하지 않습니다 .
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
소요 Matcher
직접 그것을 유형을 통과 할 수 있도록 인수한다. 대신 다음을 사용해야합니다 .isA<T>()
Matcher
expect(products, emitsError(isA<AuthException>()));
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다