Testing: Fakes
● Intermediate3 min
In a test, files, the network, the clock and your own capabilities are fakes: nothing real is touched.
use std.fs
fn save(note: String) -> String ! FsError uses Fs
ensure result == note
{
try fs.write_text("notes/today.txt", note)
try fs.read_text("notes/today.txt")
}The program ran and printed:
kavach run fake-fs.kav
0 passed, 0 failedseed 7✓ save 200 cases1 functions 1 ✓ 0 ✗ 0 weakly tested
Each of the 200 cases gets an empty file system in memory. Some cases find the disk full, so the error path is tested too, without you writing it. The network fake answers with successes, timeouts and refusals; the clock starts at a fixed date, and sleep, after and each round of every move it on at once, taking no time.
Env is a fake too: env(name) reads no real variable. Each name is decided the first time a case reads it: not set, so the call fails, or set to generated text (empty, a word, a number, a URL or true). It stays that way for the rest of the case, and is a secret, as a real one is.