选择下拉列表的RSpec / Capybara问题

时间:2015-09-13 20:24:45

标签: ruby-on-rails ruby ruby-on-rails-4 testing rspec

目前我正在尝试在我现有的注册页面上实施测试。需要填写各种必要的区域,其中两个区域是区域位置的下拉区域。第一个是选择您所在的国家/地区,然后将为该国家/地区显示一个子区域选择器,其中包含可供选择的各个州/地区。这似乎引起了我对RSpec的一个问题,虽然不确定原因。以下是我的代码:

#Signup View
....
<p>
<%= f.label :country, "Country *", class: "input_title" %><br />
<%= f.country_select :country, {priority: %w(US CA), prompt: 'Please select a country'} %>
</p>
<p>
<%= f.label :state, "Region/State *", class: "input_title"  %><br />
<%= render partial: '/users/subregion_select', locals: {parent_region: f.object.country} %>
</p>
....

#Partial for State Selection
<div id="user_state_wrapper">
  <% parent_region ||= params[:parent_region] %> <% unless parent_region.nil? %>
  <% country = Carmen::Country.coded(parent_region) %> 
  <% end %>

  <% if country.nil? %>
    Please select a country above
  <% elsif country.subregions? %>
    <%= subregion_select(:user, :state, parent_region) %>
  <% else %>
    <%= text_field(:user, :state) %>
  <% end %>
</div>

#_spec.rb
require 'rails_helper'

RSpec.feature "Users can signup" do
  scenario "with valid attributes" do
    visit "/"
    click_link "Signup"

    fill_in "Username *", with: "TestUser"
    fill_in "First Name *", with: "Joe"
    fill_in "Last Name *", with: "Dayvie"
    fill_in "Email *", with: "joe@dayvie.com"
    fill_in "Confirm Email *", with: "joe@dayvie.com"
    select "United States", from: "Country *", match: :first
    select "Nevada", from: "Region/State *"
    fill_in "City *", with: "Las Vegas"
    fill_in "Create Password *", with: "password"
    fill_in "Confirm Password *", with: "password"

    click_button "Signup"
  end
end

#Failure from Test
Failure/Error: select "Nevada", from: "Region/State *"
Capybara::ElementNotFound:
  Unable to find select box "Region/State *"

我假设发生了这种情况,因为出现了选择框;但是,这应该有效,因为它确实选择了一个国家?如果我有任何错误,或需要更多代码,请告诉我,我可以这样做。我感谢任何意见,谢谢!

1 个答案:

答案 0 :(得分:0)

如果子区域的外观是通过javascript完成的,那么请使用js:true。

scenario "with valid attributes", js: true do

end