Skip to content

Unstub with multiple stubs doesn't fail when plan wasn't met #21

@sethgupton-mastery

Description

@sethgupton-mastery

If I run

    load 'test_helper/bats-mock/stub'

# this is the "code under test"
# it would normally be in another file
format_date() {
  date -r 222
  #foo bar
}

setup() {
  _DATE_ARGS='-r 222'
  stub date \
      "${_DATE_ARGS} : echo 'I am stubbed!'" \
      "${_DATE_ARGS} : echo 'Wed Dec 31 18:03:42 CST 1969'" 
  
  #_FOO_ARGS='bar'
  #stub foo \
  #  "${_FOO_ARGS} : echo "FOOBAR""

}

teardown() {
  unstub date
  #unstub foo
}

@test "date format util formats date with expected arguments" {
  run format_date
  [ "${lines[0]}" = "I am stubbed!" ]
  #[ "${lines[1]}" = "FOOBAR" ]

  # result="$(format_date)"
  # [ "$result" == 'Wed Dec 31 18:03:42 CST 1969' ]
}

It will fail because the plan for date is met due to date only being called once.

However if I run

    load 'test_helper/bats-mock/stub'

# this is the "code under test"
# it would normally be in another file
format_date() {
  date -r 222
  foo bar
}

setup() {
  _DATE_ARGS='-r 222'
  stub date \
      "${_DATE_ARGS} : echo 'I am stubbed!'" \
      "${_DATE_ARGS} : echo 'Wed Dec 31 18:03:42 CST 1969'" 
  
  _FOO_ARGS='bar'
  stub foo \
    "${_FOO_ARGS} : echo "FOOBAR""

}

teardown() {
  unstub date
  unstub foo
}

@test "date format util formats date with expected arguments" {
  run format_date
  [ "${lines[0]}" = "I am stubbed!" ]
  [ "${lines[1]}" = "FOOBAR" ]

  # result="$(format_date)"
  # [ "$result" == 'Wed Dec 31 18:03:42 CST 1969' ]
}

Then it will succeed even though the plan for date still isn't met.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions