Skip to content

Test doubles

A test double replaces a real object in a test. Your code calls the double, and the double returns the values you chose. The test then runs without a mail server, a network or a database.

Core\Test::double builds an object for an interface. You give an anonymous function for each method:

<?nvs
// This program tests a class with a test double in place of a real mail server.
interface Mailer
{
public function send(string $to, string $text): bool;
}
class Signup
{
public static function welcome(Mailer $mailer, string $email): string
{
if (!$mailer->send($email, 'Welcome!')) {
return 'mail failed';
}
return 'mail sent';
}
}
// `Core\Test::double` builds a `Mailer`. Each method is an anonymous function.
Mailer $mailer = Core\Test::double<Mailer>({
send: fn(string $to, string $text): bool => $to != 'bad@example.com',
});
// This prints `mail sent` and then `mail failed`.
echo Signup::welcome($mailer, 'ada@example.com'), "\n";
echo Signup::welcome($mailer, 'bad@example.com'), "\n";
// `assertCalled` checks how the code used the double.
Core\Test::assertCalled($mailer, Mailer::send, { times: 1, with: ['ada@example.com', 'Welcome!'] });
echo "the welcome mail was sent once to ada\n";
Output
mail sent
mail failed
the welcome mail was sent once to ada

Core\Test::assertCalled checks that a method was called, how many times, and with which arguments. Core\Test::assertNeverCalled checks that a method was not called at all.

A test can pass a double only to a class that takes the interface as a parameter. A class that creates its own mailer inside a method cannot get a double.

  • Core\Test::partial wraps a real object and replaces only some of its methods.
  • Core\Test::answerHttp gives a fixed response for a URL. Core\Http\Client then gets this response and sends nothing over the network. Core\Test::sentHttp lists the requests your code sent.

A test with #[Test(at: "2026-03-01T09:00:00Z")] runs with the clock fixed at that time. Core\Time::now() returns that time. Core\Test::advance(1h) moves the clock one hour forward.

Under at:, Core\Time::sleep does not wait. Core\Time::sleep(1h) ends at once, and the clock is then one hour later. A test of a retry, a wait or a timeout therefore takes microseconds.

Core\Task::all runs several tasks at the same time. When two of them sleep, the one with the shorter sleep wakes first. The deadline of Core\Task::all uses the same fixed clock.

The fixed clock also decides when a value in Core\Cache::memory expires. After Core\Test::advance moves the clock past the value’s time to live, get returns null. In the same way, a queue job with a delay is due once the clock passes that delay.

A test without at: uses the real clock, and a sleep really waits.

Under nvs test, Core\Mail::send sends nothing and opens no connection. It still checks the addresses and the [mail] settings in nvs.toml. Core\Test::sentMail returns the messages your code sent in this test, in the order sent. Each one is a Core\Test\SentMail with to(), subject(), text() and the other fields.

Core\Test::logs returns the log records written in this test. Each one is a Core\Test\LogRecord with level(), message() and fields(). The level setting in the [log] block of nvs.toml decides which records are written. A record below that level is not in the list.

Each test starts with two empty lists. sentMail and logs contain what the test itself did. They also contain what each request and each Core\Test::run of the test did.

  • Do give a class its services as parameters, so a test can pass a double.
  • Do check the calls that matter, such as the address a mail was sent to.
  • Don’t replace the class you are testing. Replace only what it talks to.

Read every method in Reference: Core\Test.