Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Fix asserts on test doubles in subshell
- Allow interpolating arguments in data providers output
- Deprecate `# data_provider` in favor of `# @data_provider`
- Allow `assert_have_been_called_with` to check arguments of specific calls

## [0.19.1](https://github.com/TypedDevs/bashunit/compare/0.19.0...0.19.1) - 2025-05-23

Expand Down
16 changes: 10 additions & 6 deletions docs/test-doubles.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,26 +102,30 @@ function test_failure() {
:::

## assert_have_been_called_with
> `assert_have_been_called_with "expected" "spy"`
> `assert_have_been_called_with "expected" "spy" [call_index]`

Reports an error if `callable` is not called with `expected`.
Reports an error if `spy` is not called with `expected`. When `call_index` is
provided, the assertion checks the arguments of that specific call (starting at
1). Without `call_index` it checks the last invocation.

::: code-group
```bash [Example]
function test_success() {
spy ps

ps foo bar
ps foo
ps bar

assert_have_been_called_with "foo bar" ps
assert_have_been_called_with "foo" ps 1
assert_have_been_called_with "bar" ps 2
}

function test_failure() {
spy ps

ps bar foo
ps bar

assert_have_been_called_with "foo bar" ps
assert_have_been_called_with "foo" ps 1
}
```
:::
Expand Down
22 changes: 19 additions & 3 deletions src/test_doubles.sh
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function spy() {
export "${variable}_params_file"="$params_file"

eval "function $command() {
echo \"\$*\" > '$params_file'
echo \"\$*\" >> '$params_file'
local _c=\$(cat '$times_file')
_c=\$((_c+1))
echo \"\$_c\" > '$times_file'
Expand Down Expand Up @@ -86,14 +86,30 @@ function assert_have_been_called() {
function assert_have_been_called_with() {
local expected=$1
local command=$2
local third_arg="${3:-}"
local fourth_arg="${4:-}"

local index=""
local label=""
if [[ -n $third_arg && $third_arg =~ ^[0-9]+$ ]]; then
index=$third_arg
label="${fourth_arg:-$(helper::normalize_test_function_name "${FUNCNAME[1]}")}"
else
label="${third_arg:-$(helper::normalize_test_function_name "${FUNCNAME[1]}")}"
index="$fourth_arg"
fi

local variable
variable="$(helper::normalize_variable_name "$command")"
local file_var="${variable}_params_file"
local params=""
if [[ -f "${!file_var-}" ]]; then
params=$(cat "${!file_var}")
if [[ -n $index ]]; then
params=$(sed -n "${index}p" "${!file_var}")
else
params=$(tail -n 1 "${!file_var}")
fi
fi
local label="${3:-$(helper::normalize_test_function_name "${FUNCNAME[1]}")}"

if [[ "$expected" != "$params" ]]; then
state::add_assertions_failed
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/test_doubles_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -137,3 +137,13 @@ function test_mock_called_in_subshell() {

assert_same "2024-05-01" "$result"
}

function test_spy_called_with_different_arguments() {
spy ps

ps first_a first_b
ps second

assert_have_been_called_with "first_a first_b" ps 1
assert_have_been_called_with "second" ps 2
}
Loading