水豚选择(“单选按钮”)不起作用

问题描述:

我的观点的快照:

<%= form_for @request do |f| %>
  <div class="form-group">
    <%= f.radio_button(:item, "Snow/waterproof shell (upper)") %>
    <%= f.label(:item, "Snow/waterproof shell (upper)") %>
    </br>
    <%= f.radio_button(:item, "Headlamp") %>
    <%= f.label(:item, "Headlamp") %>
  </div>

在我编写Rspec集成测试文件(spec / requests / requests_spec.rb)时,请注意,选择单选按钮是表单的一部分,用户在其中请求列表中的项目,并且测试是针对提交后的结果页面的,该页面应指示用户所请求的项目。我正在使用gem'rspec-rails','2.13.1'

Yet on my Rspec integration test file (spec/requests/requests_spec.rb), when I write (note the choose radio button is a part of the form where the user requests an item from a list, and the test is for the resulting page after submission, which should indicate the item that the user requested). I'm using gem 'rspec-rails', '2.13.1'

describe "Requests" do
  subject { page }

  describe "new request" do
    before { visit root_path }

    describe "with valid information" do
      before do 
        choose("Snow/waterproof shell (upper)")
        click_button submit
      end

      it { should have_content("Snow/waterproof shell (upper)")
    end
  end
end

我总是会收到错误:

←[31mFailure/Error:←[0m ←[31mchoose("Snow/waterproof shell (upper)")←[0m
←[31mCapybara::ElementNotFound←[0m:
←[31mUnable to find radio button "Snow/waterproof shell (upper)"←[0m
←[36m # ./spec/requests/requests_spec.rb:24:in `block (4 levels) in <top (required)>'←[0m

与我尝试使用select( Headlamp)或其他任何选项相同。有什么想法聪明的人吗?这似乎很简单...

Same if I try with choose("Headlamp") or any other option. Any thoughts smart people? This seemed like something that would be so easy...

我已经多次遇到过这个问题。如果您根据其在dom中的ID选择表单元素,则更为可靠:

I've had this issue a number of times. If you choose form elements based on their ID in the dom it's far more reliable:

before do 
  choose 'request_item_headlamp'
  click_button submit
end

我不知道是什么就知道另一个单选按钮会出现rails。只需在chrome中右键单击它,检查元素,然后将元素ID剪切并粘贴到测试中即可。

I can't tell without looking what ID rails would come up with for the other radio button. Just right click it in chrome, inspect element, and cut and paste the element ID into your test.