我想測試從水豚的collection_select元素選擇一個值,由於某種原因,填充collection_select的數據不存在運行rspec時,但它是當運行rails應用程序。collection_select沒有填充rspec測試與水豚使用FactoryGirl
實施例:
HTML定義
<%= form_for(@notification) do |f| %>
<%= f.label :device, "Select a Device to notify:" %>
<%= f.collection_select :device_id, Device.all, :id, :device_guid, prompt: true %>
<% end %>
rspec的定義
describe "NotificationPages" do
subject { page }
let(:device) { FactoryGirl.create(:device) }
let(:notification) { FactoryGirl.create(:notification, device: device) }
describe "new notification" do
before { visit new_notification_path }
let(:submit) { "Create Notification" }
describe "with valid information" do
before do
select(device.device_guid, from: 'notification_device_id')
fill_in "Message", with: "I am notifying you."
end
it "should create a notification" do
expect { click_button submit }.to change(Notification, :count).by(1)
end
end
end
end
當運行測試時,得到以下錯誤消息:
Capybara::ElementNotFound: cannot select option, no option with text 'device_guid' in select box 'notification_device_id'
看起來像collection_select中的Device.all
調用在測試過程中沒有返回任何內容。任何關於我在做什麼的想法都是錯誤的?
感謝, 佩裏
就是這樣。非常感謝你的解釋。 – Dowling