2012-10-19 84 views
0

我試圖使用rspec來測試從選擇標記中選擇一個值。 select標籤應該列出數據庫中每個用戶的所有名稱。它在我瀏覽網站時起作用。但測試似乎沒有看到FactoryGirl創建的用戶名:Rails Rspec測試html選項

的錯誤是:

cannot select option, no option with text 'Person 4' in select box 'receiver_id' 

這裏是message_pages的部分規格:

let(:user) { FactoryGirl.create(:user) } 
let(:other_user) { FactoryGirl.create(:user) } 
before do 
    select other_user.name, :from => 'message[receiver_id]' 
    fill_in "Subject", with: "Hello" 
    fill_in "Body", with: "It's time to have fun!" 
end 

這裏的HTML我的看法輸出:

<label for="message_receiver_id">Receiver</label> 
<select id="message_receiver_id" name="message[receiver_id]"><optionvalue="12">John</option> 
<option value="13">Tom</option> 
<option value="9">Jay</option> 
<option value="14">Bob</option></select> 
+0

我曾經選擇user.name代替,並將其傳遞。顯然other_user沒有被創建?我正在使用FactoryGirl音序器。 – yeenow123

+0

你可以用'let!'來避免懶惰的評價 –

回答

2

這看起來像一個懶惰的評估問題,在使用時很常見。從documentation

注意,讓懶惰評估:它不是評估的第一次定義了方法被調用到。你可以用let!在每個示例之前強制該方法的調用。

因此,無論userother_user,纔會創建當你第一次給他們打電話,如果你已經呈現在頁面然後爲時已晚。嘗試將let調用切換到let!,然後查看是否可以解決問題。

(雖然你說,切換到user.name的作品。你可以發佈什麼其他的初始化代碼是spec這很有趣?)

+0

我認爲你是對的。我實際上在選擇測試之前調用了用戶,但我從來沒有調用other_user。謝謝!現在我知道懶惰評估是 – yeenow123

+0

這將解釋爲什麼'用戶'工作,但'other_user'沒有。很高興答案幫助! –