Ruby   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ruby-on-rails – 取消选中check_box只能使用rspec / capybara测试大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个嵌套的表单,有4个复选框.目前,一切都在浏览器中工作,但我无法获取capybara测试取消选中复选框并保存.

使用Rails 4.2.2和最新版本的capaybara-webkit和rspec

setTings.html.erb

<%= f.fields_for :preferences do |f| %>
    <div class="email-notifications-holder">
      <div class="email-holder">
        <%= f.label :new_match,"GetTing a new match each week" %>
        <%= f.check_box :new_match,class: "checkbox new_match_email" %>
      </div>
      <div class="email-holder">
        <%= f.label :match_reminder,"New matches Thursday reminder",class: "match_reminder_email" %>
        <%= f.check_box :match_reminder,default: true,class: "checkbox"   %>
      </div>
      <div class="email-holder">
        <%= f.label :accepted_match,"A Glassbreakers accepted a match",class: "accepted_match_email" %>
        <%= f.check_box :accepted_match,class: "checkbox"   %>
      </div>
      <div class="email-holder">
        <%= f.label :new_@R_197_8798@ge,"Received a new @R_197_8798@ge",class: "new_@R_197_8798@ge_email" %>
        <%= f.check_box :new_@R_197_8798@ge,class: "checkbox"  %>
      </div>
    </div>
  <% end %>

edit_account_spec.rb

it "allows the user to opt out of new match email",:js do
    user = create(:user)
    preferences = create(:preference,user: user)
    sign_in(user)

    visit edit_user_path(user)
    click_tab(t("edit_user.tabs.setTings"))

    find(:css,"#user_preferences_attributes_0_new_match").set(false)

    within "#button-container" do
      page.find('.save.main-save-button-edit').trigger('click')
    end



    visit edit_user_path(user)
    click_tab(t("edit_user.tabs.setTings"))

    user.preferences.reload
    new_match_email_checkbox = find(".new_match_email")
    expect(new_match_email_checkbox.checked?).to be_falsey
  end

我试过点击它,取消选中它,检查它,触发点击它,将它包裹在一个内部块中,重新加载数据库等.

new_match_email_checkbox = find(".new_match_email")
      within(".email-notifications-holder") do
      page.uncheck('GetTing a new match each week')
    end

    new_match_email_checkbox.set(false)

解决方法

现在,当您保存用户的个人资料时,您必须保存板载技能,否则当您尝试单击保存按钮时会抛出错误消息.

用户控制器的一部分

def update
    if update_current_user?(user_params)
      redirect_to user_path(current_user)
    else
      flash["notice"] =
        "Please choose 3 induStries,fields and years of experience."
      redirect_to edit_user_path(current_user)
    end
  end

  private

  def update_current_user?(update_params)
    skills_chosen?(update_params[:user_onboard_skills_attributes]) &&
      current_user.update(update_params)
  end

使用save_and_open_page,错误警报没有出现,因此不清楚发生了什么.我可以通过在运行测试时跟踪日志来调试这个:

tail -f log/test.log

大佬总结

以上是大佬教程为你收集整理的ruby-on-rails – 取消选中check_box只能使用rspec / capybara测试全部内容,希望文章能够帮你解决ruby-on-rails – 取消选中check_box只能使用rspec / capybara测试所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。