You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
First of all, I would like to thank @ko1nksm for this great framework. When it comes to shell unit testing, ShellSpec really is the best tool for the task.
What I noticed in my unit tests is that sometimes, I want to verify that a mock has been called with certain arguments.
If a mocked function or command only had a single argument, I would just store it in a variable (using the %preserve directive) and check the value in the expectation. That's easy and works well.
But for example, when mocking the "git" command, it becomes more cumbersome to distinguish between the sub commands (git add, git commit, git push, ...).
This extension enables bash-based ShellSpec tests to capture mock invocations and to verify expectations about these invocations.
Describe 'capture_invocation in a simple mock'
It 'captures an invocation and its arguments'
Mock git
capture_invocation git "$@"
End
When run git commit "-m""Initial commit"
The number of mocks should equal 1
The first mock should have received arguments git commit "-m""Initial commit"
End
End
The extension consists of:
The capture_invocation function to store the command name and the supplied arguments.
A ShellSpec subjectnumber of mocks (aliases are number of invocations, count of mocks, and count of invocations) that counts how many mocks have been invoked.
A ShellSpec subjectmock (alias: invocation) to select an invocation for verification matching.
A ShellSpec matcherhave received arguments to match the selected invocation against expected arguments.
Under the hood, it stores the arguments in an array variable (SHELLPEC_EXT_CAPTURED_INVOCATIONS) and the start indices of invocations into it in another array variable (SHELLSPEC_EXT_INVOCATION_START_INDICES). Basically, this emulates a two-dimensional array.
Since the scripts I test are all written in bash, this extension is also implemented in bash. If anyone wants to put themselves through the hoops of emulating these arrays in a POSIX-compliant way for sh, please feel free to contribute and maybe even merge it into ShellSpec.
The easiest way to use the extension is via the Docker image mgrafl/shellspec-ext-invocation (which is based on shellspec/shellspec:kcov):
docker run --rm -t -v ".:/src" mgrafl/shellspec-ext-invocation
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hi,
First of all, I would like to thank @ko1nksm for this great framework. When it comes to shell unit testing, ShellSpec really is the best tool for the task.
What I noticed in my unit tests is that sometimes, I want to verify that a mock has been called with certain arguments.
If a mocked function or command only had a single argument, I would just store it in a variable (using the
%preserve
directive) and check the value in the expectation. That's easy and works well.But for example, when mocking the "git" command, it becomes more cumbersome to distinguish between the sub commands (
git add
,git commit
,git push
, ...).I made a small ShellSpec extension called "shellspec-ext-invocation", that I would like to share with you:
https://github.com/mgrafl/shellspec-ext-invocation
This extension enables bash-based ShellSpec tests to capture mock invocations and to verify expectations about these invocations.
The extension consists of:
capture_invocation
function to store the command name and the supplied arguments.number of mocks
(aliases arenumber of invocations
,count of mocks
, andcount of invocations
) that counts how many mocks have been invoked.mock
(alias:invocation
) to select an invocation for verification matching.have received arguments
to match the selected invocation against expected arguments.Under the hood, it stores the arguments in an array variable (
SHELLPEC_EXT_CAPTURED_INVOCATIONS
) and the start indices of invocations into it in another array variable (SHELLSPEC_EXT_INVOCATION_START_INDICES
). Basically, this emulates a two-dimensional array.Since the scripts I test are all written in
bash
, this extension is also implemented inbash
. If anyone wants to put themselves through the hoops of emulating these arrays in a POSIX-compliant way forsh
, please feel free to contribute and maybe even merge it into ShellSpec.The easiest way to use the extension is via the Docker image mgrafl/shellspec-ext-invocation (which is based on
shellspec/shellspec:kcov
):Beta Was this translation helpful? Give feedback.
All reactions