Ruby on rails ActionController::通过RSpec测试对水豚进行无效身份验证

Ruby on rails ActionController::通过RSpec测试对水豚进行无效身份验证,ruby-on-rails,rspec,capybara,factory-bot,Ruby On Rails,Rspec,Capybara,Factory Bot,我的一个RSpec测试是在单击按钮发送电子邮件时抛出ActionController::InvalidAuthenticationToken错误 我尝试补充: skip_before_action :verify_authenticity_token, only: [:email_expiration2] if Rails.env.test? 在控制器中,但随后会处理错误并将其重新路由到404页 我感到困惑的是,测试在手动进行时,即从用户的角度浏览站点时,效果与预期一致。我可以点击按钮发送电子

我的一个RSpec测试是在单击按钮发送电子邮件时抛出ActionController::InvalidAuthenticationToken错误

我尝试补充:

skip_before_action :verify_authenticity_token, only: [:email_expiration2] if Rails.env.test?
在控制器中,但随后会处理错误并将其重新路由到404页

我感到困惑的是,测试在手动进行时,即从用户的角度浏览站点时,效果与预期一致。我可以点击按钮发送电子邮件,我会收到我想要的“已发送提醒!”页我也有其他的测试,这些测试遵循完全相同的程序和格式,适用于网站中涉及电子邮件的其他区域,并且它们按照预期工作

这是失败的测试用例:

specify 'I can remind coaches of their certificate expiration' do
  login_as @user
  @test_user = FactoryBot.create :user, email: 'test@running.com', password: 'testtest', first_name: 'Kirito', last_name: 'Adler'
  @test_role = FactoryBot.create :role, role_name: 'Volunteer'
  FactoryBot.create :coach, DBS: '12-12-2018', appl_status: true, user_id: @test_user.id
  FactoryBot.create :user_role, role_id: @test_role.id, user_id: @test_user.id
  visit '/email_application_status'
  expect(page).to have_no_content 'Kirito Adler'
  visit '/email_expiration'
  expect(page).to have_content 'Kirito Adler'
  expect(page).to have_content 'Expired (12-12-2018)'
  #save_and_open_page
  #puts page.body
  click_button 'KiritoAdler'                       <-- this throws error
  expect(page).to have_content 'Reminder sent!'    <-- this fails 
end
关联视图的一部分:

- @all_coaches.each do |user|
      - coach = Coach.where(:user_id => user.id).first
        = simple_form_for :recipent, url: reminder_sent_path, method: :post do |f|
          %tr
            %td
            = user.first_name
            = user.last_name
            %p
            = Role.find(user.user_roles.first.role_id).role_name
            %p
            Certificate expiration:
            - if !coach.DBS.nil?
              - @expiration = (((coach.DBS).to_time - @timenow) / 24.hour).ceil
              - if @expiration >= 31
                = f.label :expiration, "#@expiration", :style => "font-size:18px;color:green"
                days
              - elsif (@expiration > 1) && (@expiration < 31)
                = f.label :expiration, "#@expiration", :style => "font-size:18px;color:orange"
                days
              - elsif (@expiration == 1) || ((coach.DBS).to_time == @time)
                = f.label :expiration, "1 day", :style => "font-size:18px;color:red"
              - elsif @expiration < 1
                = f.label :expiration, "Expired", :style => "font-size:18px;color:red"
              = "(" + coach.DBS + ")"
              %br
              %br
              = f.hidden_field :expiration, :value => @expiration
              = f.hidden_field :user_id, :value => coach.id
              = f.button :submit, "Email reminder", id: user.first_name + user.last_name, :style => "background-color:black;color:white"
              %br
-@all_coach.each do|用户|
-coach=coach.where(:user\u id=>user.id)
=简单表单适用于:收件人,url:提醒发送路径,方法::post do | f|
%tr
%运输署
=user.first\u name
=user.last_name
%p
=Role.find(user.user\u roles.first.Role\u id).Role\u name
%p
证书到期:
-如果!coach.DBS.nil?
-@expiration=((coach.DBS.to_time-@timenow)/24.hour.ceil)
-如果@expiration>=31
=f.label:expiration,“#@expiration”,:style=>“字体大小:18px;颜色:绿色”
天
-elsif(@expiration>1)和(@expiration<31)
=f.label:expiration,“#@expiration”,:style=>“字体大小:18px;颜色:橙色”
天
-elsif(@expiration==1)| |((coach.DBS).to_time==time)
=f.标签:过期,“1天”,样式=>“字体大小:18px;颜色:红色”
-elsif@expiration<1
=f.标签:过期,“过期”,样式=>“字体大小:18px;颜色:红色”
=“(“+coach.DBS+”)”
%溴
%溴
=f.hidden_字段:expiration,:value=>@expiration
=f.hidden\u字段:user\u id,:value=>coach.id
=f.按钮:提交,“电子邮件提醒”,id:user.first\u name+user.last\u name,:style=>“背景颜色:黑色;颜色:白色”
%溴
我希望测试能够找到“已发送提醒!”单击电子邮件按钮后,在页面上显示

- @all_coaches.each do |user|
      - coach = Coach.where(:user_id => user.id).first
        = simple_form_for :recipent, url: reminder_sent_path, method: :post do |f|
          %tr
            %td
            = user.first_name
            = user.last_name
            %p
            = Role.find(user.user_roles.first.role_id).role_name
            %p
            Certificate expiration:
            - if !coach.DBS.nil?
              - @expiration = (((coach.DBS).to_time - @timenow) / 24.hour).ceil
              - if @expiration >= 31
                = f.label :expiration, "#@expiration", :style => "font-size:18px;color:green"
                days
              - elsif (@expiration > 1) && (@expiration < 31)
                = f.label :expiration, "#@expiration", :style => "font-size:18px;color:orange"
                days
              - elsif (@expiration == 1) || ((coach.DBS).to_time == @time)
                = f.label :expiration, "1 day", :style => "font-size:18px;color:red"
              - elsif @expiration < 1
                = f.label :expiration, "Expired", :style => "font-size:18px;color:red"
              = "(" + coach.DBS + ")"
              %br
              %br
              = f.hidden_field :expiration, :value => @expiration
              = f.hidden_field :user_id, :value => coach.id
              = f.button :submit, "Email reminder", id: user.first_name + user.last_name, :style => "background-color:black;color:white"
              %br