Search the site
REFERENCE
Test mail, HTTP calls, jobs and models without a network or a server.
Each DV.Test fake returns an object you can assert on.
ON THIS PAGE
A test with fakes
The fakes
Make records with generated factories
Run one kind of test
Reset between tests
Status
import 'package:docs_samples/dartvel_client/dartvel_client.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
// Declares the hosts in pubspec.yaml, which the app does at startup.
setUp(configureDartvelHttp);
tearDown(DVHttp.reset);
test('the welcome email is sent', () async {
final DVMemoryMailProvider mail = DV.Test.fakeMail();
await DV.Notifications.mail.send(const DVMailMessage(
from: DVMailAddress('hello@example.com'),
to: <DVMailAddress>[DVMailAddress('ada@example.com')],
subject: 'Welcome',
text: 'Hi Ada',
));
expect(mail.sent.single.subject, 'Welcome');
});
test('a payment is verified against a stubbed host', () async {
final DVHttpFake http = DV.Test.fakeHttp(<String, DVHttpStub>{
'paystack': DVHttpStub.json(<String, Object?>{'status': true}),
});
final Response response =
await DV.Http.host('paystack').get('/transaction/verify/ref-1');
expect(response.status, 200);
expect(http.calls, hasLength(1));
});
test('jobs run in memory', () async {
DV.Test.fakeQueue();
DV.Jobs.register<String>((String name) async {});
await DV.Jobs.dispatch<String>('hello');
expect(await DV.Jobs.pending(), hasLength(1));
});
}Copy code to clipboard
dartvel test
dartvel test --watchCopy code to clipboard
DV.Test.fakeMail()
Gives you: DVMemoryMailProvider, with .sent
DV.Test.fakeNotifications()
Gives you: DVMemoryNotificationProvider, with .sent
DV.Test.fakeHttp({...})
Gives you: DVHttpFake, with .calls, answering from DVHttpStub
DV.Test.fakeQueue()
Gives you: An in-memory queue
DV.Test.fakeDatabase()
Gives you: MemoryDVDatabaseAdapter
DV.Test.fakeStorage()
Gives you: DVMemoryFileStorageAdapter
DV.Test.fakeAuthUser()
Gives you: A DVAuthUser for asUser(...)
DV.Test.withSecrets({...}, body)
Gives you: Secrets for the length of body
DV.Test.fakeAI()
Gives you: LocalDVAIAdapter, with no provider called
DV.Test.fakeNativeBinding(name, handler)
Gives you: An answer for one DV.Platform binding
DV.Test.fakeWindowing(capability)
Gives you: The windowing a test declares, instead of the host's
DV.Test.fake3D() and DV.Test.fakeXR(...)
Gives you: A 3D scene without a GPU, and a fake XR device
DVHttpStub has json, text, status, timeout, error and sequence.
A host with no stub fails the call, so a test never reaches the network.
There is no cache fake: in a test DV.Cache is in memory. Call DV.Cache.delete(DVCache.all) in setUp.
setUp(ProductFactory.resetSequence);
test('each product the factory makes is its own record', () {
final List<Product> products =
const ProductFactory(published: false).createMany(3);
expect(products.map((Product p) => p.slug).toSet(), hasLength(3));
expect(products.every((Product p) => !p.published), isTrue);
});Copy code to clipboard
Every @DVModel gets a Factory class with create, createMany and admin.
Ids, emails and slugs carry a sequence, so two records never share one.
resetSequence starts the count again, so a snapshot comes out the same on every run.
dartvel test e2e
dartvel test golden --update-goldens
dartvel test --total-shards 4 --shard-index 0Copy code to clipboard
unit (the default)
Looks in: test
e2e
Looks in: test/e2e, then integration_test
golden
Looks in: test/golden or test/goldens
native
Looks in: test/native, test/ffi or test/jni
accessibility
Looks in: test/accessibility or test/a11y
release
Looks in: test/release, then the e2e folders, then test
A mode with no tests says so and lists where it looked. It never runs the whole suite under that mode's name.
DV.Test has resetQueues, resetMail, resetNotifications, resetPolicies, resetCacheTags, resetAuth, resetStorage, resetAI and resetNativeBindings. Call DVHttp.reset after an HTTP fake.
Partial
Spec section: Testing
FSL-1.1-MIT licensed. Built with Dartvel.
Dartvel is made by
To the bottom