2017-01-20 3 views
1

私は部分的にテストする必要があります。部分は、特定のアクション、そのFacebookのメッセージボックスのような何かによってレンダリングされます。私はすぐに私はそれを使用するの悪い習慣であることを聞く必要はありませんインスタンス変数でpartialをテストする方法

Failure/Error: if @items.count > 0 

    ActionView::Template::Error: 
     undefined method `count' for nil:NilClass 

を取得するので、私はそれを実行し、私はそれは私が何をしたいん知っている

describe 'partials/_partial.js.erb' do 
    it 'displays stuff' do 
    render 
    end 
end 

:私のテストでは、このようになります部分的にインスタンス変数がある、それはすでにそこにあり、私はそれを使用する必要があります。どうすればここに@itemsを設定できますか?

UPDATE:

コントローラのアクションは、次のようになります

def controller_method 
    @items = items_method 
    render_version(:partial => "partials/_partial.js.erb") 
end 
+0

コントローラのアクションを投稿できますか? – Gaston

+0

@Gastonそこに – Leo

+0

あなたはテストがコントローラを呼んでいると確信していますか?テンプレートをレンダリングするだけだと思います。 – Gaston

答えて

2

あなたはRSpecのを使用しているように見えます。その場合、テスト中に定義したインスタンス変数は、(部分的かどうかにかかわらず)ビューに使用できます。

describe "users/_messages" do 
    before(:each) do 
    # This is available in your view. 
    @items = [] 
    end 

    it "renders without error when @items is empty" do 
    # Will pass, @items is available 
    expect { render }.to_not raise_error 
    end 

    it "shows a count of how many messages there are" do 
    # You can modify it before rendering 
    @items << Message.new 
    render 

    expect(rendered).to have_content "You have 1 message" 
    end 
end 
関連する問題