Today I was digging through some unit tests in our flutter project that seemed to be failing on my machine but not necessarily in other places like our build pipeline. The problem was that we had some calls to async methods which were not being awaited properly. I fixed those up and they uncovered a bunch of more serious problems in our tests. We were calling out to validate a phone number with libphonenumber and now we were actually awaiting the call properly we saw this error
1 | [master ≡ +0 ~2 -0 !]> flutter test |
As it turns out libphonenumber is actually a native implementation wrapped up with flutter. To communicate with this native code isn't possible in a test environment so it needs to be mocked. This can be done by mocking the channel.
In the setUp() for the unit tests I added a call to setMockMethodCallHandler like so
1 | const _channel = const MethodChannel('codeheadlabs.com/libphonenumber'); |
With this call in place I was able to run the test without issue.