Skip to content

Commit ca71c26

Browse files
authored
Merge pull request #408 from TypedDevs/chore/spy-been-called-diff-args
Support specify which call to assert in have_been_called_with
2 parents 623499d + 0a12eb0 commit ca71c26

File tree

4 files changed

+40
-9
lines changed

4 files changed

+40
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
- Fix asserts on test doubles in subshell
66
- Allow interpolating arguments in data providers output
77
- Deprecate `# data_provider` in favor of `# @data_provider`
8+
- Allow `assert_have_been_called_with` to check arguments of specific calls
89

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

docs/test-doubles.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,26 +102,30 @@ function test_failure() {
102102
:::
103103

104104
## assert_have_been_called_with
105-
> `assert_have_been_called_with "expected" "spy"`
105+
> `assert_have_been_called_with "expected" "spy" [call_index]`
106106
107-
Reports an error if `callable` is not called with `expected`.
107+
Reports an error if `spy` is not called with `expected`. When `call_index` is
108+
provided, the assertion checks the arguments of that specific call (starting at
109+
1). Without `call_index` it checks the last invocation.
108110

109111
::: code-group
110112
```bash [Example]
111113
function test_success() {
112114
spy ps
113115

114-
ps foo bar
116+
ps foo
117+
ps bar
115118

116-
assert_have_been_called_with "foo bar" ps
119+
assert_have_been_called_with "foo" ps 1
120+
assert_have_been_called_with "bar" ps 2
117121
}
118122

119123
function test_failure() {
120124
spy ps
121125

122-
ps bar foo
126+
ps bar
123127

124-
assert_have_been_called_with "foo bar" ps
128+
assert_have_been_called_with "foo" ps 1
125129
}
126130
```
127131
:::

src/test_doubles.sh

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ function spy() {
5252
export "${variable}_params_file"="$params_file"
5353

5454
eval "function $command() {
55-
echo \"\$*\" > '$params_file'
55+
echo \"\$*\" >> '$params_file'
5656
local _c=\$(cat '$times_file')
5757
_c=\$((_c+1))
5858
echo \"\$_c\" > '$times_file'
@@ -86,14 +86,30 @@ function assert_have_been_called() {
8686
function assert_have_been_called_with() {
8787
local expected=$1
8888
local command=$2
89+
local third_arg="${3:-}"
90+
local fourth_arg="${4:-}"
91+
92+
local index=""
93+
local label=""
94+
if [[ -n $third_arg && $third_arg =~ ^[0-9]+$ ]]; then
95+
index=$third_arg
96+
label="${fourth_arg:-$(helper::normalize_test_function_name "${FUNCNAME[1]}")}"
97+
else
98+
label="${third_arg:-$(helper::normalize_test_function_name "${FUNCNAME[1]}")}"
99+
index="$fourth_arg"
100+
fi
101+
89102
local variable
90103
variable="$(helper::normalize_variable_name "$command")"
91104
local file_var="${variable}_params_file"
92105
local params=""
93106
if [[ -f "${!file_var-}" ]]; then
94-
params=$(cat "${!file_var}")
107+
if [[ -n $index ]]; then
108+
params=$(sed -n "${index}p" "${!file_var}")
109+
else
110+
params=$(tail -n 1 "${!file_var}")
111+
fi
95112
fi
96-
local label="${3:-$(helper::normalize_test_function_name "${FUNCNAME[1]}")}"
97113

98114
if [[ "$expected" != "$params" ]]; then
99115
state::add_assertions_failed

tests/unit/test_doubles_test.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,13 @@ function test_mock_called_in_subshell() {
137137

138138
assert_same "2024-05-01" "$result"
139139
}
140+
141+
function test_spy_called_with_different_arguments() {
142+
spy ps
143+
144+
ps first_a first_b
145+
ps second
146+
147+
assert_have_been_called_with "first_a first_b" ps 1
148+
assert_have_been_called_with "second" ps 2
149+
}

0 commit comments

Comments
 (0)