Skip to main content

Mocking

This Bats helper library provides command mocking functionality for BATS.

It allows to mock commands and check how they were called.

This is a very powerful feature that allows to test complex scenarios as unit tests.

note

To run multiple mock assertions in a more convenient way, check out the step runner helper.

Acknowledgments

This functionality is based on the bats-mock project. A special thank you to the contributors for their original work.

Usage

setup() {
setup_mock
}

# Example to test the notify.sh script that uses curl to send a notification to external system.
@test "Notify" {
app_id="9876543210"

# Mock curl command.
mock_curl="$(mock_command "curl")"

# Setup mock responses for curl call with specific arguments in notify.sh.
mock_set_output "${mock_curl}" "12345678910-1234567890-${app_id}-12345" 1
mock_set_output "${mock_curl}" "201" 2

run ./notify.sh
assert_success

# Single line mock assertion example.
assert_equal "-s -X GET https://api.example.com/v2/applications.json" "$(mock_get_call_args "${mock_curl}" 1)"
# Multi-line mock assertion example.
assert_equal '-X POST https://api.example.com/v2/applications/9876543210/deployments.json -d {
"deployment": {
"description": "example description",
}
}' "$(mock_get_call_args "${mock_curl}" 2)"
}

Setup functions

mock_create

  • Description: Creates a mock program that can be executed and tracked.
  • Arguments: None
  • Global Variables: Uses BATS_MOCK_TMPDIR if set, otherwise BATS_TMPDIR
  • Outputs: Path to the created mock file
  • Usage:
    mock=$(mock_create)

setup_mock

Setup mock support. Call this function from your test's setup() method.

mock_command

  • Description: Mock provided command.
  • Arguments:
    • Mocked command name
  • Outputs: Path to created mock file.
  • Usage:
    mock_command "ls"

mock_set_output

  • Description: Sets the output of the mock.
  • Arguments:
    • Path to the mock
    • Output or - for STDIN
    • Index of the call (optional)
  • Usage:
    mock_set_output "${mock}" "some output"

mock_set_status

  • Description: Sets the exit status of the mock.
  • Arguments:
    • Path to the mock
    • Status
    • Index of the call (optional)
  • Usage:
    mock_set_status "${mock}" 0

mock_set_side_effect

  • Description: Sets shell code to be executed when the mock runs.
  • Arguments:
    • Path to the mock
    • Side effect (shell code) or - for STDIN
    • Index of the call (optional)
  • Usage:
    mock_set_side_effect "${mock}" "echo 'side effect executed' > /tmp/log"

Assertion functions

mock_get_call_args

  • Description: Returns the arguments line the mock was called with.
  • Arguments:
    • Path to the mock
    • Index of the call (optional)
  • Outputs: Arguments line
  • Usage:
    assert_equal "expected_arg1 expected_arg2" "$(mock_get_call_args "${mock}" 1)"

mock_get_call_num

  • Description: Returns the number of times the mock was called.
  • Arguments:
    • Path to the mock
  • Outputs: Number of calls
  • Usage:
    assert_equal "1" "$(mock_get_call_num "${mock}")"

mock_get_call_user

  • Description: Returns the user the mock was called with.
  • Arguments:
    • Path to the mock
    • Index of the call (optional)
  • Outputs: User name
  • Usage:
    assert_equal "expected_user" "$(mock_get_call_user "${mock}" 1)"

mock_get_call_env

  • Description: Returns the value of the environment variable the mock was called with.
  • Arguments:
    • Path to the mock
    • Variable name
    • Index of the call (optional)
  • Outputs: Variable value
  • Usage:
    assert_equal "expected_val1" "$(mock_get_call_env "${mock}" VAR_NAME 1)"