Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mocking a stream and whenCalling doesnt mock #311

Open
kazerdira opened this issue Jun 28, 2024 · 1 comment
Open

mocking a stream and whenCalling doesnt mock #311

kazerdira opened this issue Jun 28, 2024 · 1 comment

Comments

@kazerdira
Copy link

kazerdira commented Jun 28, 2024

i have this method

ResultStream<List<HelpResponse>> getResponses({required String requestId}) {
return _remoteDataSource
.getResponses(requestId)
.map<EitherResult<List<HelpResponse>>>((response) {
try {
final helpResponses =
response.map((model) => model as HelpResponse).toList();
return Right(helpResponses);
} catch (e) {
return Left(
ServerFailure(
message: 'Error processing response: $e',
statusCode: 1507,
),
);
}
}).handleError((Object error) {
if (error is FirebaseException) {
return Left<Failure, dynamic>(
ServerFailure(
message: error.toString(),
statusCode: 501,
),
);
}
return Left<Failure, dynamic>(
ServerFailure(
message: 'Unknown error occurred',
statusCode: 500,
),
);
});
}

and this is my test

test('getRequests handles errors [FirebaseException]', () async {
final collection = mockFirebaseFirestore.collection('responses');
whenCalling(Invocation.method(#snapshots, null))
.on(collection)
.thenThrow(FirebaseException(
plugin: 'cloud_firestore',
message: 'Test error',
code: 'test_error',
));

// final stream = dataSource.getResponses(requestId);

expect(
() => dataSource.getResponses(requestId),
emitsError(
isA<ServerException>()
.having((e) => e.message, 'message', contains('Test error'))
.having((e) => e.statusCode, 'statusCode', 501),
),
);
});
});

but i am getting this error

Expected: should emit an error that <Instance of 'ServerException'> with message: contains 'Test error' and statusCode: 'test_error'
Actual: <Closure: () => Stream<List<HelpResponseModel>>>
Which: was not a Stream or a StreamQueue

@atn832
Copy link
Owner

atn832 commented Jun 28, 2024

You are right, throwing errors is not supported on collection.snapshots. See the list of supported methods here: https://github.com/atn832/fake_cloud_firestore?tab=readme-ov-file#mocking-exceptions. If you need exceptions thrown when calling snapshots, feel free to propose a PR and I'll review it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants