2016-12-05 17 views
1

我在rails上的ruby单元测试有问题(rails v。5.001)。我使用devise和cancancan进行授权。用户需要在测试单元中登录,但是如何在不重定向到http://www.example.com/users/sign_in的情况下执行此操作?下面是代码:控制器测试:<302: Found>重定向到<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 \ 
     "[email protected]" => 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> 

回答

2

问题是你的用户是没有得到证实。

您需要

confirmed_at = Time.now 

传递到您所创建的用户。那会阻止你重定向。

+0

如果我添加include_Warden ::测试::助手和使用should_get_new login_as(@heiko),那么出现这样的错误:AppointmentsControllerTest#test_should_get_new: UncaughtThrowError:未捕获抛出:监狱长 测试/控制器/ appointments_controller_test.rb:26:在'在'中嵌入' – Peter

+0

@Peter在用户模型中是否有'confirmable'? – DMH

+0

yes在models/user.rb中我确认:devise:database_authenticatable,:可注册, :可恢复的,:可记住的,:可跟踪的,:可验证的,:可确认的 – Peter