1

スタブに問題があります。スタブがどのように機能するかを誤解しているに違いないと思います。

スタブは作成されたコンテキスト内にのみ存在しますか? それは私の予想ですが、私の経験では、コンテキスト内でメソッドをスタブすると、別のコンテキストにまだ存在します。

私のコントローラーテストはこれに似ています:

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

2番目の例では、期待しているときにcontroller.load_articlesまだ返されます['article'][]

私はこれにあまりにも長い間立ち往生してきました。どんな助けでも大歓迎です!

4

1 に答える 1

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
于 2013-02-01T03:36:15.273 に答える