diff --git a/docs/test-doubles.md b/docs/test-doubles.md index 5f12f69c..c7163a44 100644 --- a/docs/test-doubles.md +++ b/docs/test-doubles.md @@ -156,3 +156,26 @@ function test_failure() { } ``` ::: + +## assert_not_called +> `assert_not_called "spy"` + +Reports an error if `spy` has been executed at least once. + +::: code-group +```bash [Example] +function test_success() { + spy ps + + assert_not_called ps +} + +function test_failure() { + spy ps + + ps + + assert_not_called ps +} +``` +::: diff --git a/src/test_doubles.sh b/src/test_doubles.sh index c9c02e19..29ba8102 100644 --- a/src/test_doubles.sh +++ b/src/test_doubles.sh @@ -141,3 +141,9 @@ function assert_have_been_called_times() { state::add_assertions_passed } + +function assert_not_called() { + local command=$1 + local label="${2:-$(helper::normalize_test_function_name "${FUNCNAME[1]}")}" + assert_have_been_called_times 0 "$command" "$label" +} diff --git a/tests/unit/test_doubles_test.sh b/tests/unit/test_doubles_test.sh index 22040c67..a789d0a0 100644 --- a/tests/unit/test_doubles_test.sh +++ b/tests/unit/test_doubles_test.sh @@ -147,3 +147,21 @@ function test_spy_called_with_different_arguments() { assert_have_been_called_with "first_a first_b" ps 1 assert_have_been_called_with "second" ps 2 } + +function test_spy_successful_not_called() { + spy ps + + assert_not_called ps +} + +function test_spy_unsuccessful_not_called() { + spy ps + + ps + + assert_same \ + "$(console_results::print_failed_test "Spy unsuccessful not called" "ps" \ + "to has been called" "0 times" \ + "actual" "1 times")" \ + "$(assert_not_called ps)" +}