使用RSpec测试在水豚上的ActionController :: InvalidAuthenticityToken

时间:2019-04-12 13:19:36

标签: ruby-on-rails rspec capybara factory-bot

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

我尝试添加:

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

已声明所有适当的工厂都可以进行测试,并且所有HTML元素均具有正确的ID。

这是控制器中失败的方法:

def email_expiration2
  @user_id = params[:recipent][:user_id]
  @user = User.find(@user_id)
  @expiration = params[:recipent][:expiration]
  UserMailer.new_expiration_mail(@user.email, @user.first_name, @expiration).deliver
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

我希望测试能够找到“已发送提醒!”单击电子邮件按钮后,在页面上显示。

0 个答案:

没有答案