platform/packages/test
2024-06-23 12:09:26 +08:00
..
example Updated auth 2021-09-29 15:40:27 +08:00
lib Fixed mismatch messages 2023-11-05 17:34:41 +08:00
test Fixed mismatch messages 2023-11-05 17:34:41 +08:00
.gitignore Updated authors and license 2021-05-15 16:48:20 +08:00
analysis_options.yaml Updated test 2021-09-25 22:51:11 +08:00
AUTHORS.md Updated authors and license 2021-05-15 16:48:20 +08:00
CHANGELOG.md Updated to support SDK 3.3.0 2024-06-23 12:09:26 +08:00
LICENSE Updated license 2021-09-25 22:55:32 +08:00
melos_angel3_test.iml Added melos 2022-03-19 09:37:28 +08:00
pubspec.yaml Updated to support SDK 3.3.0 2024-06-23 12:09:26 +08:00
README.md Updated repository links 2023-12-25 11:45:10 +08:00

Angel3 Test

Pub Version (including pre-releases) Null Safety Gitter License

Testing utility library for Angel3 framework.

TestClient

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);

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(...);

Matchers

Several Matchers are bundled with this package, and run on any package:http Response, not just those returned by Angel.

void test('foo', () async {
    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', 'angel'), // Assert header present with value
        hasHeader('foo', ['bar', 'baz']), // ... Or multiple values
        hasBody(), // Assert non-empty body
        hasBody('{"foo":"bar"}') // Assert specific body
    ]));
});

void test('error', () async {
    var res = await client.get('/error');
    expect(res, isAngelHttpException());
    expect(res, isAngelHttpException(statusCode: 404, message: ..., errors: [...])) // Optional
});

hasValidBody is one of the most powerful Matchers in this library, because it allows you to validate a JSON body against a validation schema. Angel3 provides a comprehensive validation library that integrates tightly with the matcher package that you already use for testing.

test('validate response', () async {
    var res = await client.get('/bar');
    expect(res, hasValidBody(Validator({
        'foo': isBoolean,
        'bar': [isString, equals('baz')],
        'age*': [],
        'nested': someNestedValidator
    })));
});