2013-01-06 36 views
0

我有以下测试:RSpec的消息的期望没有得到满足,但方法被称为

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 

我可以用一对夫妇binding.pry的验证调用升降被调用。但RSpec没有看到它。我哪里错了。我想确保正在测试的方法在正确的情况下调用其他方法。有没有另一种方法来做到这一点?

回答

0

我倾向于永远不会在before块预期。我看到它的方式,before块真的只是设置你正在测试的情况,而不是干掉预期。当您将exporter.should_receive(:get_method).at_least(:once).and_call_original移出before块并将其分发到3个it块(在这三种情况中的每一种情况下根据需要进行编辑)时会发生什么情况?这可能与其他should_receive调用冲突。

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

+0

没有骰子。在具体的块中,我得到了与call_original相同的失败。这是一种私人方法。不过,我想通过测试,以便我知道未来是否会破坏测试。要么通过改变其作出决定的条件,要么通过修改方法而不意识到我也修改了行为。 – LeakyBucket

+0

文件中是否还有其他存根(可能位于顶部)?这个陷阱会让你经常... – Gabe

+0

不是,只有两个let语句:let(:exporter){Export :: Exporter.new'data',[:split,{:to_s => [:split] }]} let(:formats){{:xls => Export :: Formatters :: XLSFormatter,:csv => Export :: Formatters :: CSVFormatter}} – LeakyBucket

相关问题