Skip to content

Commit

Permalink
Merge pull request #235 from TypedDevs/feat/unmock-2
Browse files Browse the repository at this point in the history
unmock all test doubles after each test
  • Loading branch information
Chemaclass authored May 21, 2024
2 parents 734ca24 + 39be998 commit 7d05411
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Add multi-invokers; consolidate parameterized-testing documentation
- Add `fail()` function
- Remove all test mocks after each test case

## [0.11.0](https://github.com/TypedDevs/bashunit/compare/0.10.1...0.11.0) - 2024-03-02

Expand Down
2 changes: 1 addition & 1 deletion docs/test-doubles.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Allows you to override the output of a callable.
```bash [Example]
function test_example() {
function code() {
ps a | grep bash
ps a | grep bash
}

mock ps<<EOF
Expand Down
7 changes: 7 additions & 0 deletions src/runner.sh
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ function runner::run_test() {
"$function_name" "$@" 2>&1 1>&3
runner::run_tear_down
runner::clear_mocks
state::export_assertions_count
)

Expand Down Expand Up @@ -229,6 +230,12 @@ function runner::run_tear_down() {
helper::execute_function_if_exists 'tear_down'
}

function runner::clear_mocks() {
for i in "${!MOCKED_FUNCTIONS[@]}"; do
unmock "${MOCKED_FUNCTIONS[$i]}"
done
}

function runner::run_tear_down_after_script() {
helper::execute_function_if_exists 'tear_down_after_script'
}
Expand Down
18 changes: 18 additions & 0 deletions src/test_doubles.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
#!/bin/bash

declare -a MOCKED_FUNCTIONS=()

function unmock() {
local command=$1

for i in "${!MOCKED_FUNCTIONS[@]}"; do
if [[ "${MOCKED_FUNCTIONS[$i]}" == "$command" ]]; then
unset "MOCKED_FUNCTIONS[$i]"
unset -f "$command"
break
fi
done
}

function mock() {
local command=$1
shift
Expand All @@ -11,6 +25,8 @@ function mock() {
fi

export -f "${command?}"

MOCKED_FUNCTIONS+=("$command")
}

function spy() {
Expand All @@ -24,6 +40,8 @@ function spy() {
eval "function $command() { ${variable}_params=(\"\$*\"); ((${variable}_times++)) || true; }"

export -f "${command?}"

MOCKED_FUNCTIONS+=("$command")
}

function assert_have_been_called() {
Expand Down
19 changes: 19 additions & 0 deletions tests/acceptance/mock_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

#
# Make sure that the `runner::clear_mocks()` is being called,
# removing the mocks and spies from the first test
#
function test_runner_clear_mocks_first() {
mock ls echo foo
assert_equals "foo" "$(ls)"

spy ps
ps foo bar
assert_have_been_called_times 1 ps
}

function test_runner_clear_mocks_second() {
assert_not_equals "foo" "$(ls)"
assert_have_been_called_times 0 ps
}

0 comments on commit 7d05411

Please sign in to comment.