Jump to content

The ultimate community for Ruby on Rails developers.


Photo

cucumber/capybara can't select n option

cucumber capybara testing rails4 ruby2.0

  • Please log in to reply
7 replies to this topic

#1 Vell

Vell

    Inspector

  • Members
  • 78 posts
  • LocationWashington, DC

Posted 03 March 2014 - 01:04 AM

Hello all,

 

I have written a cucumber test for a blog application that I am writing (using rails 4 and ruby 2.0). I am having trouble getting my test to select an option in a drop down list. 

 

The test i have is:

  Scenario: Creating a valid blog
    When the subject is filled in
    And the body is filled in
    And a category is selected
    When the form is submitted
    Then a success notification is presented
    And a record is written to the database
 

The test I am having issues with is:

When(/^a category is selected$/) do
  select 'Personal', from: 'blog_category'
end
What is happening is, my code is hitting the validates presence of category line in the model and of course tells me:
1 error prohibited this blog from being saved: Category can't be blank

Can anyone tell me if there is another way to select an option in a drop down list? Or somehow see why the select method is not working in capybara?

 

 



#2 danielhanke

danielhanke

    Signalman

  • Members
  • 12 posts
  • LocationCologne, Germany

Posted 03 March 2014 - 06:49 AM

Vell, you want to select the options-value, not the text-label.

 

try 

find("option[value='Personal']").click

 

if the value is not accessible.



#3 Vell

Vell

    Inspector

  • Members
  • 78 posts
  • LocationWashington, DC

Posted 03 March 2014 - 08:43 PM

I still get the same error message. Its so weird.  The only other thing I can think of is that my validation is somehow in correct but I can't see how that is the case.

 

class Blog < ActiveRecord::Base
  CATEGORY_LIST = ['Personal', 'Ruby on Rails', 'Professional', 'Announcement'].sort
  validates :subject, :body, :category, presence: :true
  has_many :blog_comments
end

and my view has:

<div class="field">
  <%= f.label :category %><br>
  <%= f.select :category, Blog::CATEGORY_LIST, prompt: true %>
</div>

Could I try using xpath? I am not familiar with using it but if that could work how would I implement it? 



#4 Vell

Vell

    Inspector

  • Members
  • 78 posts
  • LocationWashington, DC

Posted 05 March 2014 - 04:07 AM

I actually just tried this using rails server and its giving me the same error.  It's so weird. Everything appears to be correct even down to the strong params.



#5 Vell

Vell

    Inspector

  • Members
  • 78 posts
  • LocationWashington, DC

Posted 06 March 2014 - 11:47 PM   Best Answer

OK. I don't get it. But its working now. I just moved my validation of category to a different spot in the validation and everything worked. Very strange.

validates :subject, :body, :category, presence: :true

Didn't work but

validates :subject, :category, :body, presence: :true

works



#6 danielhanke

danielhanke

    Signalman

  • Members
  • 12 posts
  • LocationCologne, Germany

Posted 07 March 2014 - 08:49 AM

is the colon before true (":true") intended? 



#7 Vell

Vell

    Inspector

  • Members
  • 78 posts
  • LocationWashington, DC

Posted 07 March 2014 - 01:40 PM

Yes it is intentional. Rails complains if the colon is not there from what I have seen. The syntax would be the same as

validates :subject, :category, :body, :presence => :true

or

validates_presence_of :subject, :category, :body, :message => "can't be blank"


#8 danielhanke

danielhanke

    Signalman

  • Members
  • 12 posts
  • LocationCologne, Germany

Posted 07 March 2014 - 05:35 PM

i just never saw the key-value as symbol in any project and use the boolean values everywhere without having any troubles, that is why i wondered ;) but fine if it works ;) thanks!







Also tagged with one or more of these keywords: cucumber, capybara, testing, rails4, ruby2.0

1 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users


    Google (1)