2011-11-18 72 views
2

所以我想用Rspec和Factory Girl來測試一些視圖,但我不知道如何正確指定工廠視圖。我在網上看到的所有信息都使用了stubbing,儘管stubbing沒問題,但我仍想保持rspec的一致性。使用Rspec視圖的工廠女孩​​

我想爲一個版本模型使用工廠,並在呈現頁面時將其與頁面關聯,但我所考慮的一切似乎都已被破壞。這裏有一個排序荒謬的例子中:

require 'spec_helper' 

describe "catalog/editions/rationale.html.haml" do 
    before do 
    @edition = Factory.create(:edition) 
    assign(:editions, @edition) 
    render :action => 'rationale', :id => @edition 
    end 
    context "GET rationale" do 
    it "should not have the preamble to the United States constitution" do 
     rendered.should_not contain(/We the People of the United States/) 
    end 
    end 
end 

在此,我試圖改變渲染:行動=>「理由」,:ID => @edition只是渲染,以及其他類似的調整,以渲染動作。我只是不知道在哪裏開始工廠幫助查看。任何幫助將不勝感激。

版本:

的Rails 3.0.10

RSpec的2.7

Factory_Girl 2.2

+0

您能否詳細說明問題的具體內容?測試失敗的地方在哪裏? – jaredonline

+0

另外,您使用的是Rails,FactoryGirl和RSpec的版本? – jaredonline

回答

6

我認爲錯誤是@editions期待一個數組,所以你應該寫

assign(:editions, [@edition]) 

或者,否則,如果它應該是英格爾元素,你應該寫:

assign(:edition, @edition) 

其次,除非你的看法是部分是預計的變量,你應該只寫

render 

你不必給動作,RSpec的知道哪些看法要從describe進行渲染,並且該視圖不檢索任何數據(因此您不必設置該ID),則只需使用assigns正確設置實例變量即可。測試視圖只不過是渲染視圖。

希望這會有所幫助。

+0

你是完全正確的,這是我一開始就有的,但它不起作用。我發現我做錯了,我沒有初始化控制器/視圖中所需的所有變量。感謝您的反饋! –

2

現在你已經得到了答案,停止編寫視圖規範。他們不需要很難寫,而且他們真的沒有足夠的測試來完成任何有價值的工作。使用Cucumber進行視圖和控制器測試。