passsy / prefer-fakesr3
Prefer Fakes Over Mocks
PublicUse fakes with in-memory state instead of mockito or mocktail mocks, because their state is easy to inspect while debugging.
tests, run by the registry7 passed · slowest 4.6 ms
✓fires2.9 ms
a mocktail import and a Mock subclass fire
write test/api_test.dartfired: line 1, line 3
import 'package:mocktail/mocktail.dart';
class MockApi extends Mock implements Api {}✓fires2.7 ms
a @GenerateMocks annotation fires
write test/repo_test.dartfired: line 1
([Repo])
void main() {}✓fires3.0 ms
adding mockito to the pubspec fires
write pubspec.yamlfired: line 4
namedemo
dev_dependencies
test^1.25.0
mockito^5.4.4✓quiet2.4 ms
a mockito dependency that was already there stays quiet
write pubspec.yaml · wrote line 3stayed quiet
namedemo
dev_dependencies
test^1.25.0
mockito^5.4.4✓quiet2.5 ms
a hand-written fake stays quiet
write test/api_test.dartstayed quiet
class FakeApi implements Api {
final List<String> calls = [];
}✓fires4.6 ms
points at the principle when the user asks for a mock in a Dart project
prompt · in fixtures/dart_appfired: "Mock"
Mock the API in this test✓quiet3.3 ms
stays quiet on the same words outside a Dart project
promptstayed quiet
Mock the API in this testdetector.test.tseach detect() call runs the real detectorEdit
import { expect, test } from "vitest";
import { detect, edit, prompt, source, write } from "@wellactually/sdk/v3/test";
test("a mocktail import and a Mock subclass fire", () => {
const code = source`
import 'package:mocktail/mocktail.dart';
class MockApi extends Mock implements Api {}
`;
expect(detect(write("test/api_test.dart", code))).toEqual([
{ line: 1 },
{ line: 3 },
]);
});
test("a @GenerateMocks annotation fires", () => {
expect(detect(write("test/repo_test.dart", "@GenerateMocks([Repo])\nvoid main() {}"))).toEqual([{ line: 1 }]);
});
const pubspec = source`
name: demo
dev_dependencies:
test: ^1.25.0
mockito: ^5.4.4
`;
test("adding mockito to the pubspec fires", () => {
expect(detect(write("pubspec.yaml", pubspec))).toEqual([{ line: 4 }]);
});
test("a mockito dependency that was already there stays quiet", () => {
expect(detect(edit("pubspec.yaml", pubspec, { written: " test: ^1.25.0" }))).toEqual([]);
});
test("a hand-written fake stays quiet", () => {
const code = source`
class FakeApi implements Api {
final List<String> calls = [];
}
`;
expect(detect(write("test/api_test.dart", code))).toEqual([]);
});
const dartApp = { project: "fixtures/dart_app" };
test("points at the principle when the user asks for a mock in a Dart project", () => {
expect(detect(prompt("Mock the API in this test"), dartApp)).toEqual([{ match: "Mock", depth: "pointer" }]);
});
test("stays quiet on the same words outside a Dart project", () => {
expect(detect(prompt("Mock the API in this test"))).toEqual([]);
});