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

Mock repositories #73

Merged
merged 6 commits into from
Apr 10, 2024
Merged

Mock repositories #73

merged 6 commits into from
Apr 10, 2024

Conversation

yoannLafore
Copy link
Collaborator

@yoannLafore yoannLafore commented Apr 7, 2024

Hello,
This pull request aims to provide the boiler plate code needed to mock the methods of the repositories.
This code is contained in the classes MockPostRepositoryService and MockUserRepositoryService.

Here is an example of how such a class can be used in a test to mock a repository method

late MockPostRepositoryService postRepository;

setUp(() {
  // Initialize the mock
  postRepository = MockPostRepositoryService();
});

test("some test", () {
  final point = ...
  final kmPostRadius = ...

  // To mock the [getNearPosts] method when it is called with particular parameters
   when(postRepository.getNearPosts(point, kmPostRadius)).thenAnswer(
        // Here, it is mocked to return an empty list
        (_) async => [], 
   );

  // From this point on, when the "postRepository.getNearPosts(point, kmPostRadius)" is called, the mock overrides
  // enters into action and an empty list will be returned
});

This PR also provides a class to easily generate mock data for UserFirestore which can be useful when testing.
This class is MockUserFirestore located under test/models/database/user.

Finally, MockFirestorePost has been renamed to MockPostFirestore as well as being moved to test/models/database/user for consistency.

Important note:

For the mock of the repositories, the way the code is written can misleadingly make believe that a particular value will be returned whithout having to mock it in the tests with when(...) ....
This, is not the case, if a method from the mock repository is called without having properly set up the when(...) ... before, an exception will be thrown.

For example, in the MockUserRepositoryService, we have the following override:

  @override
  Future<bool> doesUserExist(UserIdFirestore uid) {
    return super.noSuchMethod(
      Invocation.method(#doesUserExist, [uid]),
      returnValue: Future.value(false),
    );
  }

In our test code we could be tempted to simply call:

userRepository.doesUserExist(...);

expecting it to return it false.

This wont work and will throw an exception.
The correct way is:

when(userRepository.doesUserExist(...)).thenAnswer(
  () async {return false;}
);

userRepository.doesUserExist(...);

@yoannLafore yoannLafore marked this pull request as ready for review April 7, 2024 23:26
@yoannLafore yoannLafore self-assigned this Apr 7, 2024
@yoannLafore yoannLafore requested a review from Aderfish April 7, 2024 23:27
@gruvw
Copy link
Collaborator

gruvw commented Apr 8, 2024

I think most of those newly created methods should be static. You should not need an instance of MockPostFirestore or similar.

@gruvw
Copy link
Collaborator

gruvw commented Apr 8, 2024

Also, it is a good idea to mimic the directory structure of the original lib in the tests 👍

@yoannLafore
Copy link
Collaborator Author

Thanks for your feedback @gruvw, you're right, the methods of MockPostFirestore and MockUserFirestore can be static.
This is corrected in commits 14fec27 and 0887b80 👍

@yoannLafore yoannLafore requested a review from gruvw April 8, 2024 16:28
Copy link
Collaborator

@Aderfish Aderfish left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good job, looks good to me

@yoannLafore yoannLafore merged commit 0d6efd6 into main Apr 10, 2024
2 checks passed
@yoannLafore yoannLafore deleted the mock-repositories branch April 10, 2024 07:48
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

Successfully merging this pull request may close these issues.

3 participants