Skip to content

Add example that demonstrates how to mock commands called by functions under test #156

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions doc/contributors.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ contributed in some way or another to shunit2.
- Rocky Bernstein
- [rugk](https://github.com/rugk)
- wood4321 (of code.google.com)
- [Rainer Poisel](https://github.com/rpoisel)
12 changes: 12 additions & 0 deletions examples/lib.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# available as examples/lib.inc

myFunc() {
local msg="${1}"

local log
log="[$(date)] ${msg}"
echo "${log}"
if [ -n "${LOG_FILE_PATH}" ]; then
echo "${log}" 2>/dev/null >> "${LOG_FILE_PATH}"
fi
}
68 changes: 68 additions & 0 deletions examples/mock_cmd_complex_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/bin/sh
#
# shUnit2 example for mocking commands.
#
# This example demonstrates a more flexible mechanism for mocking external
# dependencies such as commands or functions.
#
# In this example, the behavior of the mocking function `date` can be changed
# by defining the logic in a global variable `__ACTION`. Please note that this
# approach would not work when executing tests in parallel due to race conditions
# when reading/writing the global `__ACTION` variable.

. ./lib.inc

__ACTION="true"

# dynamic mock (overrides original dependency)
date() {
eval "${__ACTION}"
}

#dedicated function implementing the mock logic
dying_date_func() {
exit 1
}

testMyFuncDateDies() {
__ACTION="dying_date_func"
LOG_FILE_PATH="/tmp/01.log"

local result rc exists contents
result=$(myFunc "some message")
rc=$?

assertEquals 0 "${rc}"
assertEquals "[] some message" "${result}"

exists=0
[ -e "/tmp/01.log" ] && exists=1
assertEquals 1 "${exists}"
contents=$(cat /tmp/01.log)
assertEquals "[] some message" "${contents}"
}

testMyFuncDoesSomethingMeaningful() {
__ACTION="echo \"now\""
LOG_FILE_PATH="/tmp/02.log"

local result rc exists contents
result=$(myFunc "some message")
rc=$?

assertEquals 0 "${rc}"
assertEquals "[now] some message" "${result}"

exists=0
[ -e "/tmp/02.log" ] && exists=1
assertEquals 1 "${exists}"
contents=$(cat /tmp/02.log)
assertEquals "[now] some message" "${contents}"
}

setUp() {
cp /dev/null /tmp/01.log
cp /dev/null /tmp/02.log
}

. ../shunit2
50 changes: 50 additions & 0 deletions examples/mock_cmd_simple_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#!/bin/sh
#
# shUnit2 example for mocking commands.
#
# This example demonstrates a simple mechanism for mocking external
# dependencies such as commands or functions.
#
# In this example, the mocking function `date` behaves the same for
# all tests.

. ./lib.inc

# static mock (overrides original dependency)
date() {
echo "now"
}

testMyFuncMissingPath() {
unset LOG_FILE_PATH

local result rc
result=$(myFunc "some message")
rc=$?

assertEquals 0 "${rc}"
assertEquals "[now] some message" "${result}"
}

testMyFuncHappy() {
LOG_FILE_PATH="/tmp/01.log"

local result rc exists contents
result=$(myFunc "some message")
rc=$?

assertEquals 0 "${rc}"
assertEquals "[now] some message" "${result}"

exists=0
[ -e "/tmp/01.log" ] && exists=1
assertEquals 1 "${exists}"
contents=$(cat /tmp/01.log)
assertEquals "[now] some message" "${contents}"
}

setUp() {
cp /dev/null /tmp/01.log
}

. ../shunit2