Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8445475
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:40:14+00:00 2026-06-10T09:40:14+00:00

I am trying to test selecting a value from a collection_select element with capybara

  • 0

I am trying to test selecting a value from a collection_select element with capybara and for some reason, the data to populate the collection_select isn’t there when running the rspec, but it is when running the rails app.

Example:

html definition

<%= 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 definition

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

When running the test, I get the following error message:

Capybara::ElementNotFound: cannot select option, no option with text 'device_guid' in select box 'notification_device_id'

It looks like the Device.all call in the collection_select isn’t returning anything during the test. Any ideas as to what I am doing wrong?

Thanks,
Perry

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T09:40:16+00:00Added an answer on June 10, 2026 at 9:40 am

    At the moment you visit new_notification_path there is no device in the database. This happens because let is lazy-evaluated, so the method it defines is called the first time you invoke it, which in your test happens only when you execute the select(device.device_guid ...) statement.

    To make sure the device is created before you visit the path, you can just call ‘device’ in your before block.

    before do
      device
      visit new_notification_path
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to insert some data from a XML document into a variable table.
Trying to test the option of replacing our internal data access layer with Entity
I trying to test an AccountController that uses DotNetOpenAuth but I am running into
I am trying to test some async methods involving the HttpClient work with vs
I'm trying to test some mailers with rspec but deliveries are always empty. Here
I am trying to test a sinatra app using minitest and capybara but get
Iam trying to create a new test project. I remember selecting the names of
I'm trying to select a value in a select element. I'm using Selenium RC
Just trying to do some quick and dirty testing. I am passing fake data
I'm selecting data from a database and returning it in a json object, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.