Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add an info about using wildcard params and functions in the README #47

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,47 @@ If you stub functions, make sure to unset them,
or the stub script wan't be called,
as the function will shadow the binstub script on the `PATH`.

### Advanced usage

You can set a global wildcard argument to allow any call: `stub cmd '* : echo "my return"'`.

You can use a function to evaluate the return statement (the function MUST be exported in the script):

```bats
load helper

# this is the "code under test"
# it would normally be in another file
format_date() {
date -r 222
}

function my_test_fct()
{
echo "This is my final stub return, receiving arguments: $*"
}
export -f my_test_fct

setup() {
_DATE_ARGS='-r 222'
stub date \
"${_DATE_ARGS} : echo 'I am stubbed!'" \
"${_DATE_ARGS} : my_test_fct \$*"
}

teardown() {
unstub date
}

@test "date format util formats date with expected arguments" {
result="$(format_date)"
[ "$result" == 'I am stubbed!' ]

result="$(format_date)"
[ "$result" == "This is my final stub return, receiving arguments: -r 222" ]
}
```

## Credits

Extracted from the [ruby-build][] test suite.
Expand Down