Create What is Mockito authored by Andrew Januszko's avatar Andrew Januszko
Mockito is a testing suite that allows us to test communication without need the server to be up. It can model the service client and the datasource, so we can do full sequence tests without needing real data or the servers being up.
# How do I do it?
First generate mock versions of all the data that you want to test.
```dart
@GenerateMocks([
\\ insert mockable classes here
])
void main() {
// do nothing, generate mocks...
}
```
Then run the `autogenerate.sh` file to create the mock files needed for testing.
Finally, create the sequence tests:
```dart
void main() {
late MockServiceClient mockServiceClient;
late CompleteObjectiveDatasource datasource;
setUp(() {
mockServiceClient = MockServiceClient();
datasource = CompleteObjectiveDatasourceHTTP(
sc: mockServiceClient,
);
});
group('Test CompleteObjectiveDatasource', () {
test('Test for valid response.', () async {
final testJSONResponse = jsonDecode('''{
"responseType": 0
}''');
final expected = CompleteObjectiveResponse.fromJson(
json: testJSONResponse,
);
when(
mockServiceClient.post(
endpoint: anyNamed('endpoint'),
body: anyNamed('body'),
),
).thenAnswer((realInvocation) async => testJSONResponse);
final actual = await datasource.completeObjective(
request: const CompleteObjectiveRequest(
playerID: 0,
objectiveID: 0,
questID: 0,
),
);
verify(mockServiceClient.post(
endpoint: anyNamed('endpoint'),
body: anyNamed('body'),
));
expect(actual, expected);
});
});
}
```
\ No newline at end of file