Extension for capturing mock invocations #309
Unanswered
mgrafl
asked this question in
Show and tell
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
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