RSpec: определение множественных вызовов метода с другим аргументом каждый раз

В rspec (1.2.9), что корректный путь состоит в том, чтобы определить, что объект получит множественные вызовы метода с другим аргументом каждый раз?

Я спрашиваю из-за этого запутывающего результата:

describe Object do

  it "passes, as expected" do
    foo = mock('foo')
    foo.should_receive(:bar).once.ordered.with(1)
    foo.should_receive(:bar).once.ordered.with(2)
    foo.bar(1)
    foo.bar(2)
  end

  it "fails, as expected" do
    foo = mock('foo')
    foo.should_receive(:bar).once.ordered.with(1) # => Mock "foo" expected :bar with (1) once, but received it twice
    foo.should_receive(:bar).once.ordered.with(2)
    foo.bar(1)
    foo.bar(1)
    foo.bar(2)
  end

  it "fails, as expected" do
    foo = mock('foo')
    foo.should_receive(:bar).once.ordered.with(1)
    foo.should_receive(:bar).once.ordered.with(2)
    foo.bar(2) # => Mock "foo" received :bar out of order
    foo.bar(1)
  end

  it "fails, as expected, but with an unexpected message" do
    foo = mock('foo')
    foo.should_receive(:bar).once.ordered.with(1)
    foo.should_receive(:bar).once.ordered.with(2)
    foo.bar(1)
    foo.bar(999) # => Mock "foo" received :bar with unexpected arguments
                 # =>   expected: (1)
                 # =>         got (999)
  end

end

Я ожидал, что последнее сообщение об отказе будет "ожидаться: (2)", не "ожидаемый (1)". Я использовал rspec неправильно?

37
задан Wayne Conrad 28 December 2009 в 21:51
поделиться

1 ответ

Аналогично этому вопросу . Рекомендуемое решение - вызвать as_null_object, чтобы избежать путаницы сообщений. Итак:

describe Object do
  it "fails, as expected, (using null object)" do
    foo = mock('foo').as_null_object
    foo.should_receive(:bar).once.ordered.with(1)
    foo.should_receive(:bar).once.ordered.with(2)
    foo.bar(1)
    foo.bar(999) # => Mock "foo" expected :bar with (2) once, but received it 0 times
  end
end

Вывод не совпадает со вторым случаем (т.е. "ожидал 2, но получил 999"), но показывает, что ожидание не оправдалось.

.
34
ответ дан 27 November 2019 в 05:01
поделиться
Другие вопросы по тегам:

Похожие вопросы: