取消选中 check_box only not working with rspec / capybara tests

Uncheck check_box only not working with rspec / capybara tests

我有一个包含 4 个复选框的嵌套表单。目前,一切都在浏览器中运行,但我无法通过水豚测试取消选中复选框并保存。

使用 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, default: true, class: "checkbox"   %>
      </div>
      <div class="email-holder">
        <%= f.label :new_message, "Received a new message", class: "new_message_email" %>
        <%= f.check_box :new_message, default: true, 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

只需使用它就会取消选中复选框

   within(".email-notifications-holder") do
      page.uncheck('Getting a new match each week')
   end

但是你必须抓住这个元素来测试它。

new_match_email_checkbox = find(".new_match_email")
expect(new_match_email_checkbox.checked?).to be_falsey

注意: 我不清楚的一件事。你想让这条线工作吗?:

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

或者您是否在调用 user.preferences.reload 后尝试取消选中该复选框?