我在使用存根时遇到了一些麻烦,我认为我必须误解它们是如何工作的。
存根只存在于创建它们的上下文中吗?这是我的期望,但根据我的经验,如果我在上下文中存根方法,它仍然存在于另一个上下文中。
我的控制器测试类似于:
describe '.load_articles' do
context 'articles' do
before(:each) do
Article.stub_chain(:meth1, :meth2).and_return(['article'])
end
it 'sets articles' do
controller.load_articles.should == ['article']
end
end
context 'no articles' do
before(:each) do
Article.stub_chain(:meth1, :meth2).and_return([])
end
it 'sets article' do
controller.load_articles.should == []
end
end
end
并且第二个示例controller.load_articles
在我期待['article']
[]
我被困在这个问题上太久了;非常感谢任何帮助!
答案 0 :(得分:1)
每个示例后清除存根。你可以很容易地证明这一点:
class Numero; end
describe Numero do
context "Uno" do
before do
Numero.stub_chain(:meth1, :meth2) { 'uno' }
end
it "unos" do
Numero.meth1.meth2.should == 'uno'
end
end
context "Dos" do
before do
Numero.stub_chain(:meth1, :meth2) { 'dos' }
end
it "dosses" do
Numero.meth1.meth2.should == 'dos'
end
end
end