platform/packages/test/README.md

62 lines
2.7 KiB
Markdown
Raw Normal View History

# Protevus Test
2021-07-10 02:51:07 +00:00
2021-09-25 14:51:11 +00:00
![Pub Version (including pre-releases)](https://img.shields.io/pub/v/angel3_test?include_prereleases)
2021-05-15 08:35:27 +00:00
[![Null Safety](https://img.shields.io/badge/null-safety-brightgreen)](https://dart.dev/null-safety)
2024-07-07 15:02:49 +00:00
[![Discord](https://img.shields.io/discord/1060322353214660698)](https://discord.gg/3X6bxTUdCM)
[![License](https://img.shields.io/github/license/dart-backend/protevus)](https://github.com/dart-backend/protevus/tree/master/packages/test/LICENSE)
2016-12-10 18:15:02 +00:00
Testing utility library for Protevus framework.
2021-07-10 02:51:07 +00:00
## TestClient
2016-10-16 20:02:21 +00:00
2021-09-25 16:17:28 +00:00
The `TestClient` class is a custom `angel3_client` that sends mock requests to your server. This means that you will not have to bind your server to HTTP to run. Plus, it is an `angel3_client`, and thus supports services and other goodies. The `TestClient` also supports WebSockets. WebSockets cannot be mocked (yet!) within this library, so calling the `websocket()` function will also bind your server to HTTP, if it is not already listening. The return value is a `WebSockets` client instance (from [`package:angel3_websocket`](<https://pub.dev/packages/angel3_websocket>));
2017-03-25 15:26:00 +00:00
```dart
var ws = await client.websocket('/ws');
ws.service('api/users').onCreated.listen(...);
// To receive all blobs of data sent on the WebSocket:
ws.onData.listen(...);
```
2021-07-10 02:51:07 +00:00
## Matchers
2021-09-25 14:51:11 +00:00
Several `Matcher`s are bundled with this package, and run on any `package:http` `Response`, not just those returned by Angel.
2017-03-25 15:26:00 +00:00
```dart
2021-09-25 14:51:11 +00:00
void test('foo', () async {
2017-03-25 15:26:00 +00:00
var res = await client.get('/foo');
expect(res, allOf([
isJson({'foo': 'bar'}),
hasStatus(200),
hasContentType(ContentType.JSON),
hasContentType('application/json'),
hasHeader('server'), // Assert header present
hasHeader('server', 'protevus'), // Assert header present with value
2017-03-25 15:26:00 +00:00
hasHeader('foo', ['bar', 'baz']), // ... Or multiple values
hasBody(), // Assert non-empty body
hasBody('{"foo":"bar"}') // Assert specific body
]));
});
2021-09-25 14:51:11 +00:00
void test('error', () async {
2017-03-25 15:26:00 +00:00
var res = await client.get('/error');
expect(res, isAngelHttpException());
expect(res, isAngelHttpException(statusCode: 404, message: ..., errors: [...])) // Optional
});
```
`hasValidBody` is one of the most powerful `Matcher`s in this library, because it allows you to validate a JSON body against a validation schema. Protevus provides a comprehensive [validation library](<https://pub.dev/packages/angel3_validate>) that integrates tightly with the `matcher` package that you already use for testing.
2017-03-25 15:26:00 +00:00
```dart
test('validate response', () async {
var res = await client.get('/bar');
2023-10-27 15:52:29 +00:00
expect(res, hasValidBody(Validator({
2017-03-25 15:26:00 +00:00
'foo': isBoolean,
'bar': [isString, equals('baz')],
'age*': [],
'nested': someNestedValidator
})));
});
2021-07-10 02:51:07 +00:00
```