控制器测试:&lt; 302:Found&gt;重定向到<http:www.example.com =“”users =“”sign_in =“”>

时间:2016-12-05 12:55:17

标签: ruby-on-rails unit-testing devise cancancan

我在轨道上的ruby中进行单元测试时遇到问题(rails v.5.001)。我使用devise和cancancan进行授权。用户需要登录测试单元,但如何在不重定向到http://www.example.com/users/sign_in的情况下实现此目的?这是代码:

appointments_controller_test.rb

require 'test_helper'

class AppointmentsControllerTest < ActionDispatch::IntegrationTest
  include Devise::Test::IntegrationHelpers

  def sign_in(user)
    post user_session_path \
      "heiko@me.com"    => user.email,
      "hhdk#s0" => user.password
  end

  setup do
    @heikoAppointment = appointments(:appointment_heiko)
    @heiko = users(:user_heiko)
    sign_in(@heiko)
  end

  test "should get index" do
    get appointments_url
    assert_response :success
  end

  test "should get new" do
    sign_in(@heiko)
    get new_appointment_url
    assert_response :success
  end

这是我运行测试时遇到的错误:

Error:
AppointmentsControllerTest#test_should_get_new [C:/Users/Clemens/meindorfladen/Server/test/controllers/appointments_controller_test.rb:33]:
Expected response to be a <2XX: success>, but was a <302: Found> redirect to <http://www.example.com/users/sign_in>

1 个答案:

答案 0 :(得分:2)

问题是您的用户未得到确认。

你需要传入

confirmed_at = Time.now

给您创建的用户。然后,这将阻止您重定向。