-
Notifications
You must be signed in to change notification settings - Fork 3
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
Always verify mock arguments #29
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,13 +39,12 @@ validate-args() { | |
args="\$@" | ||
|
||
if [ "\${args}" != "${expected}" ]; then | ||
|
||
cat <<OUT | ||
cat > \$0_error <<OUT | ||
Unexpected invocation for command 'some-command': | ||
Got : <"\${args}"> | ||
Expected : <"${expected}"> | ||
OUT | ||
exit 1 | ||
exit 1 # Kept for historical reasons. Proper usage would be mock xyz --with-args "arg1 arg2" --and exit-code 1. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no really, this exit 1's goal was to break the test in case of wrong arguments. since you check that afterward now you can take that off. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Currently, it breaks scripts that invoke it when they fail because of the exit code or when they are running in set -e mode. Removing it changes the contract with the test subject. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh of course, i thought the error file lookup was after a mock but it's at the end of the test. You're right sir ;) |
||
fi | ||
EOF | ||
} | ||
|
@@ -159,5 +158,9 @@ _verify_mocks() { | |
assertion_failed "Command '${command}' was expected to be called $(_format_count ${expected_calls} "time")\nCalled : $(_format_count ${call_count} "time")" | ||
fi | ||
done | ||
|
||
for invocation in $(find ${mock} -maxdepth 1 -name "invocation_*_error" 2>/dev/null || true); do | ||
assertion_failed "$(cat $invocation)" | ||
done | ||
done | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#!/bin/bash | ||
# N.B. no `set -e` here! | ||
|
||
some-command one un | ||
some-command three trois || true # Failure here doesnt matter to the actual code. | ||
some-command two deux | ||
|
||
exit 0 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/bin/bash | ||
|
||
test_fails_when_verifying_arguments_even_if_exit_code_is_ignored() { | ||
mock some-command --with-args "one un" --once | ||
mock some-command --with-args "two deux" --once | ||
mock some-command --with-args "three trois" --once | ||
|
||
./code.sh | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,25 @@ | ||
#!/bin/bash | ||
|
||
test_fails_if_given_arguments_isnt_right() { | ||
mock some-command --with-args "one two three" | ||
test_fails_when_verifying_arguments_even_when_exit_code_is_ignored() { | ||
cp -aR ${TEST_ROOT_DIR}/../test-fixtures/mocking-args-strict/* . | ||
|
||
some-command three two one > assertion_output | ||
unset RUN_SINGLE_TEST | ||
actual=$(${TEST_ROOT_DIR}/../target/sbtest.sh verify_arguments_order) | ||
assert ${?} failed | ||
|
||
expected_error=$(cat <<-EXP | ||
Unexpected invocation for command 'some-command': | ||
Got : <"three two one"> | ||
Expected : <"one two three"> | ||
Got : <"three trois"> | ||
Expected : <"two deux"> | ||
Unexpected invocation for command 'some-command': | ||
Got : <"two deux"> | ||
Expected : <"three trois"> | ||
EXP | ||
) | ||
assert "$(cat assertion_output)" equals "${expected_error}" | ||
assert "${actual}" contains "${expected_error}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. asserting how problems are displayed is an important part of the user experience of this library, by using contains instead of equals you seem to be hiding something, what is it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The stuff being hidden is caused by the test setup, we essentially have a fake test suite and the output contains much more than the errors themselves. By using contains we can focus on the important part for that test. Otherwise, this test will break when we change, for example, the test report header or footer. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The exact text is the following:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. okay i get the extent of this now, sorry for that. The test you're replacing is kind of part of the doc on how to use mocking... yes the tests is the doc, so it's important that it's clear and understandable. Therefore may i ask that the test remains there and that your test becomes a new one at the end of the file ? And i will agree to the assert contains since this only changes stuff inside the mocking mechanism. PS. (trailing new lines :) ) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done :) |
||
} | ||
|
||
test_fails_if_2_with_args_argments_are_given() { | ||
test_fails_if_2_with_args_arguments_are_given() { | ||
mock some-command --with-args "a" --with-args "b" > error | ||
assert ${?} failed | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be
To contain pattern
cause the assert says assert contains ;)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.