Skip to content

Commit 745f37c

Browse files
authored
Merge pull request #410 from TypedDevs/codex/improve-test-doubles-experience
Add assert_not_called test double
2 parents 51dd91e + ce32d4a commit 745f37c

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

docs/test-doubles.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,26 @@ function test_failure() {
156156
}
157157
```
158158
:::
159+
160+
## assert_not_called
161+
> `assert_not_called "spy"`
162+
163+
Reports an error if `spy` has been executed at least once.
164+
165+
::: code-group
166+
```bash [Example]
167+
function test_success() {
168+
spy ps
169+
170+
assert_not_called ps
171+
}
172+
173+
function test_failure() {
174+
spy ps
175+
176+
ps
177+
178+
assert_not_called ps
179+
}
180+
```
181+
:::

src/test_doubles.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,9 @@ function assert_have_been_called_times() {
141141

142142
state::add_assertions_passed
143143
}
144+
145+
function assert_not_called() {
146+
local command=$1
147+
local label="${2:-$(helper::normalize_test_function_name "${FUNCNAME[1]}")}"
148+
assert_have_been_called_times 0 "$command" "$label"
149+
}

tests/unit/test_doubles_test.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,21 @@ function test_spy_called_with_different_arguments() {
147147
assert_have_been_called_with "first_a first_b" ps 1
148148
assert_have_been_called_with "second" ps 2
149149
}
150+
151+
function test_spy_successful_not_called() {
152+
spy ps
153+
154+
assert_not_called ps
155+
}
156+
157+
function test_spy_unsuccessful_not_called() {
158+
spy ps
159+
160+
ps
161+
162+
assert_same \
163+
"$(console_results::print_failed_test "Spy unsuccessful not called" "ps" \
164+
"to has been called" "0 times" \
165+
"actual" "1 times")" \
166+
"$(assert_not_called ps)"
167+
}

0 commit comments

Comments
 (0)