Write Unit Tests for Appwrite SDK on Flutter With Mockito | by Aditya Rohman | May, 2022

A sensible information to unit check Appwrite SDK on Flutter

Appwrite is an open-source (OSS) Backend as a Service that’s aiming to summary the complexity of widespread, complicated, and repetitive duties required for constructing a contemporary app. Appwrite can be utilized as a substitute for Firebase with a bunch of options like authentications, database, storage, features, and real-time assist. Additionally, it offers SDK for nearly all platforms like internet, Android, iOS, Flutter, and so forth.

Though we utilizing SDK for creating an app. Unit testing remains to be vital to do to check if our app is linked effectively or to not that SDK. So, on this article, I need to present you easy methods to create a unit check in your Flutter venture when utilizing Appwrite because the SDK on your backend.

In case you are new to time period unit testing, you may learn my earlier article about Flutter testing here.

Step 1 — Get Mockito Plugin

As a result of in a unit check we don’t execute an actual perform. So, to have the ability to unit check a service like Appwrite is that we have to create a mock for that service. To do this we want a plugin referred to as Mockito

Let’s add mockito to our dev_dependencies on pubspec.yaml file.

...
dev_dependencies:
flutter_test:
sdk: flutter
build_runner: ^2.1.7
mockito: ^5.1.0
...

In addition to mockito, we additionally want build_runner it as a result of mockito will generate the mock file.

Step 2 — Create Appwrite Mock

After getting the plugins we want, the subsequent step is to generate a mock of our service. For instance, I need to create a repository for authentication stuff. So, I’ll generate a mock for the Account from Appwrite SDK.

To generate a brand new mock file, we use @GenerateMocks() annotation on our check file. After that, we have to run build_runner to start out producing the mock file. This command will generate a brand new file like auth_repository_test.mocks.dart

flutter packages pub run build_runner construct

Step 3 — Write Check

Now, we’re prepared to put in writing our check. For example, I’ll write a check for the login perform. Here’s what my check file for auth_repository_test.dart appears like:

MockAccount is taken from the mock file that we generated beforehand. On the AuthRepositoryImpl, I cross the _mockAccount because the dependency.

Step 4 — Write Precise Code

The final step is to put in writing the precise repository code. Here’s what my auth_repository.dart file appears like:

By the way in which, Appwrite additionally offers AppwriteException class. It’s useful if you wish to create extra wealthy error dealing with on your app. For instance, incorrect password warning, e-mail already in use warning, and so forth.

More Posts