Skip to content

Spy exit code #600

@RevanProdigalKnight

Description

@RevanProdigalKnight

Right now bashunit::spy thing essentially creates a no-op mock that always returns 0. It would be useful to be able to add an optional exit code or mock implementation, e.g.: bashunit::spy thing 1 or bashunit::spy thing mock_thing for testing different branches in a function while also being able to make assertions on the spy itself.

Larger example of what I'm trying to do:

# function being spied upon
function thing() {
  if [ "${#1}" -lt 3 ]; then
    return 0
  else
    return 1
  fi
}

# function being tested
function do_things() {
  for word in "$@"; do
    if ! thing "$word"; then
      return 0
    fi
  done

  return 1
}

# test cases
function test_do_things() {
  bashunit::spy thing

  do_things "hi" "my" "name" "is" "bob"
  # FAILS because thing spy always returns 0
  assert_exit_code 0
  # FAILS because do_things didn't return early
  assert_have_been_called_times 3 thing
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions