Hey, for starters - Thank you so much for taking the time to make this package. Currently I'm running into an issue with unit testing.
I have a function where I use a future to hit an API, store that data as an object then store that inside hive, and then set the state to authenticated using Provider.
Is there a way to unit test hive without touching the login function?
Which login function are you referring to? Maybe you could provide a few more details.
Generally you can use the mockito package and mock the Box or LazyBox interface...
How would you use mockito with hive to mock the box? I looked online and didn't see any info on it. If you could provide examples, that would be amazing
How would you use mockito with hive to mock the box? I looked online and didn't see any info on it. If you could provide examples, that would be amazing
I have same issue, not know how to create tests, but i'm starting with tests in Flutter. I looked flutter documentation and I found this page: https://flutter.dev/docs/cookbook/testing/unit/mocking
I did not get it yet. You has a few ideia?
I did not get it yet. You has a few ideia?
You can initialize hive in temp directory without using mockito =)
If you are unit testing your own code (aka "I want to make sure that my code asks Hive to save this piece of data on a successful login"), I would suggest mocking Hive, so that you can make sure that it is being called at the proper time with the proper arguments.
If you are testing your login functionality as a whole, and not just your code as a unit (aka "When there is a successful login, I wait for my code to finish, then I check in the Hive database that I can get the proper data"), I would suggest as @TheMisir said, to initialize Hive in a temporary directory using setUp and deleting it after your tests using teardown.
However, you should not need to test Hive on its own (aka "Is Hive saving my data when I ask it to"), since the Hive package is already.
Here is StackExchange discussion about this.
Of course, testing practices vary a lot from one team to another. These are my recommendations.
TLDR: if you want to unit-test your code, and just your code, mock Hive. If you want to test the entire login functionality as a unit, initialize Hive with a temporary database. But if you do this, be careful to have a brand new database on each test run, as failing to do so may render your tests unstable.
@TheMisir I think this can be closed now.
Thanks @thib92 for detailed explanation.
Most helpful comment
If you are unit testing your own code (aka "I want to make sure that my code asks Hive to save this piece of data on a successful login"), I would suggest mocking Hive, so that you can make sure that it is being called at the proper time with the proper arguments.
If you are testing your login functionality as a whole, and not just your code as a unit (aka "When there is a successful login, I wait for my code to finish, then I check in the Hive database that I can
getthe proper data"), I would suggest as @TheMisir said, to initialize Hive in a temporary directory usingsetUpand deleting it after your tests usingteardown.However, you should not need to test Hive on its own (aka "Is Hive saving my data when I ask it to"), since the Hive package is already.
Here is StackExchange discussion about this.
Of course, testing practices vary a lot from one team to another. These are my recommendations.
TLDR: if you want to unit-test your code, and just your code, mock Hive. If you want to test the entire login functionality as a unit, initialize Hive with a temporary database. But if you do this, be careful to have a brand new database on each test run, as failing to do so may render your tests unstable.
@TheMisir I think this can be closed now.