0

I have the following test:

  describe "Exporter#get_method" do
    before(:each) do
      exporter.should_receive(:get_method).at_least(:once).and_call_original
    end

    it "should get the next terminal value" do
      exporter.send(:get_method).should == :split
    end

    it "should call descend if the current value is a hash" do
      exporter.should_receive(:descend).once

      2.times { exporter.send(:get_method) }
    end

    it "should call ascend if at the end of an array and there is a prologue" do
      exporter.should_receive(:ascend).once

      3.times { exporter.send(:get_method) }
    end
  end

I can verify with a couple of binding.pry calls that ascend and descend are being called. However RSpec isn't seeing it. Where am I going wrong. I want to make sure that the method being tested does call the other methods under the correct circumstances. Is there another way to do this?

4

1 回答 1

0

我倾向于从不将期望放在before块中。在我看来,before块实际上只是为了设置您正在测试的情况,而不是为了消除期望。当您移出exporter.should_receive(:get_method).at_least(:once).and_call_originalbefore并将其分布在 3it个块中时会发生什么(在这三种情况下根据需要对其进行编辑)?它可能与其他should_receive调用发生冲突。

另外,如果你打电话exporter.get_method而不是打电话,它会起作用exporter.send(:get_method)吗?通常,RSpec 旨在测试行为,而不是实现,如果get_method是私有方法,那么直接测试它没有任何意义。相反,您可能想要做的是为使用该私有方法的方法编写测试。否则,如果它不是私有方法,为什么要使用:send

于 2013-01-06T23:11:14.720 回答