Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

After migrating flutter code to null-safety, mock objects not accepting `any`

After the release of Flutter 2, I've migrated my code to sdk: '>=2.12.0 <3.0.0' and all codes are "sound null safety" now. But I encountered errors in unit tests with mockito 5.0.0

e.g:

when(mockClient.login(any)).thenThrow(GrpcError.unavailable());

was ok earlier, but now, the compiler shows an error under any, indicating: The argument type 'Null' can't be assigned to the parameter type 'LoginRequest'

I read this link from mockito repo but I hope there is an easier way to write tests for methods with "not nullable" arguments like before.

like image 863
Arash Avatar asked Mar 11 '21 12:03

Arash


People also ask

IS null safety good in Flutter?

Adding null safety to Dart and Flutter became a good solution to developer productivity issues and reduced the number of bugs. With null safety, now Flutter developers can specify which variables can be null and all the previously unnoticed errors will show up during static analysis.

How to migrate Flutter App to null safety?

We will Migrate flutter app to null safety by 3 simple steps. Step 1: First step is to check is the project is migratable or not, because we can't migrate all projects. Step 2: We need to migrate our dependency plugins. Step 3: Call Migrate project.

How do I migrate my code to null safety?

Welcome to null safety. This page describes how and when to migrate your code to null safety . Here are the basic steps for migrating each package that you own: Wait for the packages that you depend on to migrate. Migrate your package’s code, preferably using the interactive migration tool. Statically analyze your package’s code.

Is my dart or flutter package null safe?

For example, the Dart and Flutter core libraries are null safe, and they’re still usable by apps that haven’t migrated to null safety. This section tells you how to check and update your package’s dependencies, with the help of the dart pub outdated command in null-safety mode.

What is the migration tool for null-unsafe Dart code?

The migration tool takes a package of null-unsafe Dart code and converts it to null safety. You can guide the tool’s conversion by adding hint markers to your Dart code. Before starting the tool, make sure you’re ready:


2 Answers

When assigning the Mock object, it needs to be of the Mock object type, not the BaseClass.


@GenerateMocks(MockSpec<ITransactionRepository>(as: #MockTransactionRepository),
)
void main()
{
    ....
    ITransactionRepository baseObject = MockTransactionRepository();           // wrong
    MockTransactionRepository mockObject = MockTransactionRepository();   // right
    when(baseObject.method(any));     // results in compile error
    when(mockObject.method(any)); // OK
    ...
}

Source: https://github.com/dart-lang/mockito/issues/364

like image 151
Tom Crasset Avatar answered Oct 20 '22 13:10

Tom Crasset


See the solution here. You can use the mocktail package which makes it way easier.

With mocktail your code would become

when(() => mockClient.login(any())).thenThrow(GrpcError.unavailable());
like image 9
Pablito Avatar answered Oct 20 '22 15:10

Pablito