2015-07-13 77 views
1

我是新的測試與Ruby和我使用Rspec & FactoryGirl爲此。RSpec測試索引視圖和will_paginate

我想測試我的索引視圖,其中顯然有will_paginate

這裏是我的控制器:

def index 
    @products = Product.paginate(:page => params[:page], :per_page=>30) 
    respond_with(@products) 
end 

我有一個will_paginate基本觀點和產品表。

這裏是我的測試:

RSpec.describe "products/index", type: :view do 
    before(:each) do 
    @products = Array.new 
    31.times do 
     @products << FactoryGirl.create(:product) 
    end 
    assign(:products, @products) 
    end 

    it "renders a list of products" do 
    render 
    end 
end 

首先,是它做的正確方法:創建一個數組,然後分配給它?

糾正我,如果我錯了,但assign(:products, @products)是相當於@products = Product.paginate(:page => params[:page], :per_page=>30)爲測試

回答

1

我繞過我的問題與此代碼:

RSpec.describe "products/index", type: :view do 
    before(:each) do 
    # Create a list of 31 products with FactoryGirl 
    assign(:products, create_list(:product, 31)) 
    end 

    it "renders a list of products" do 
    allow(view).to receive_messages(:will_paginate => nil) 
    render 
    end 
end 

我接受這個解決方案的任何建議。