简体   繁体   English

Ruby on Rails:无法选中 RSpec 测试中的复选框

[英]Ruby on Rails: Cannot check the checkbox in RSpec test

I am testing my code in Ruby on Rails but cannot place checkmark in the checkbox, and no idea why.我在 Rails 上的 Ruby 中测试我的代码,但无法在复选框中放置复选标记,也不知道为什么。 I am using RSpec.我正在使用 RSpec。

In the test code I set to place a checkmark in the checkbox that have association with Task model, but got errors.在测试代码中,我设置在与任务 model 关联的复选框中放置复选标记,但出现错误。

・I confirmed 'checkbox' appears in the error page screenshot.・我确认错误页面截图中出现了“复选框”。・I get 'ElementNotFound' message and cannot place a checkmark (Error details below)・我收到“ElementNotFound”消息,无法勾选(错误详情如下)

Following are my code related to this problem.以下是我与此问题相关的代码。

Anyone can help?任何人都可以帮忙吗? Thank you.谢谢你。

#app/views/tasks/_form.html.erb

<% Label.all.each do |label| %>
   <%= form.check_box :label_ids, { multiple: true, checked: label[:checked], disabled: label[:disabled], include_hidden: false }, label[:id] %>
   <%= label.name %>
<% end %>
#spec/system/task_spec.rb 

describe 'Checking Labels' do
  before do
    FactoryBot.create(:label)
  end
  context 'When I select Label' do
    it 'Selected label appears in the show page' do
      visit new_task_path
      fill_in 'task_task_name', with: 'TEST'
    check "label1"
      click_on 'Go'
      click_on 'Detail'
      expect(page).to have_content "label1"
    end
  end
end
#factoryBot

FactoryBot.define do
  factory :label do
    name { "label1" }
  end
end
#Error message

Failure/Error: check "label1"
     
Capybara::ElementNotFound:
  Unable to find checkbox "label1" that is not disabled

Showing the erb isn't very useful because it doesn't actually fully define what HTML is produced without more info, so in the future please always provide the actual HTML produced in your test.显示erb并不是很有用,因为它实际上并没有完全定义 HTML 是在没有更多信息的情况下产生的,所以将来请始终提供在您的测试中产生的实际 HTML。

The reason this isn't working for you is because you're trying to click based on the label name, but that name isn't associated with the checkbox in any way.这对您不起作用的原因是因为您尝试基于 label 名称单击,但该名称与复选框没有任何关联。 If you look at the actual HTML of the page you'll see there's no label element with a for attribute linking it to your checkbox, and clicking on the text doesn't toggle the checkbox as it should.如果您查看页面的实际 HTML,您会看到没有带有for属性的 label 元素将其链接到您的复选框,并且单击文本不会像应有的那样切换复选框。

You probably want to be looking at the collection_check_boxes method, or manually using the label method on the form builder with something like您可能想查看collection_check_boxes方法,或者在表单构建器上手动使用label方法,例如

<% Label.all.each do |label| %>
   <%= form.check_box :label_ids, { multiple: true, checked: label[:checked], disabled: label[:disabled], include_hidden: false }, label[:id] %>
   <%= form.label :label_ids, label.name, value: label[:id] %>
<% end %>

which should create a label element associated with the check box and allow you to find the element by the label text, so `check 'label1'` will work.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM