Ruby on rails 控制器测试:<;302:已找到>;重定向到<;http://www.example.com/users/sign_in>
我对RubyonRails(rails v.5.001)中的单元测试有问题。我使用Deviate和cancancan进行授权。用户需要登录到一个测试单元,但是我如何在不重定向到的情况下实现这一点呢?代码如下: 预约\u控制器\u测试.rbRuby on rails 控制器测试:<;302:已找到>;重定向到<;http://www.example.com/users/sign_in>,ruby-on-rails,unit-testing,devise,cancancan,Ruby On Rails,Unit Testing,Devise,Cancancan,我对RubyonRails(rails v.5.001)中的单元测试有问题。我使用Deviate和cancancan进行授权。用户需要登录到一个测试单元,但是我如何在不重定向到的情况下实现这一点呢?代码如下: 预约\u控制器\u测试.rb require 'test_helper' class AppointmentsControllerTest < ActionDispatch::IntegrationTest include Devise::Test::IntegrationHe
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
需要“测试助手”
类指定ControllerTestuser.email,
“hhdk#s0”=>user.password
结束
设置
@平子约会=约会(:约会\平子)
@heiko=用户(:user_heiko)
签到(@heiko)
结束
测试“应该得到索引”吗
从url获取约会
断言:成功
结束
测试“应该得到新的”吗
签到(@heiko)
获取新的\u约会\u url
断言:成功
结束
下面是我运行测试时得到的错误:
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>
错误:
任命控制人测试应获得新的[C:/Users/Clemens/Meindorfload/Server/test/controllers/Appoints\U controller\u test.rb:33]:
预期响应为,但为重定向到
问题是您的用户没有得到确认
你需要进去
confirmed_at = Time.now
发送给您创建的用户。这将阻止你被重定向 如果我添加include_Warden::Test::Helpers并使用登录名(@heiko)在“是否应该获得新的”中,则会出现以下错误:AppointmentsControllerTest“test”是否应该获得新的:UncaughtThrower:UncaughtThrow:warden test/controllers/Appoints\u controller\u test.rb:26:in`block in'@Peter您在用户模型上有
可确认的
吗?在models/user.rb中有可确认的:designe:database\u authenticable,:registerable,:recoverable,:rememberable,:trackable,:validable,:confirmableOk因此问题是您创建的用户未确认,因此重定向。尝试在:时间添加confirmed\u。现在将添加到您创建的用户中。好的,谢谢。我现在应该把这个时间放在哪里?以(@heiko)的身份登录。在:时间确认。现在不起作用