【问题标题】:why i'm getting this error ::Expected response to be a <2XX: success>, but was a <302: Found> redirect to <http://www.example.com/users/sign_in>为什么我收到此错误 ::Expected response to be a <2XX: success>, but was a <302: Found> redirect to <http://www.example.com/users/sign_in>
【发布时间】:2019-09-09 09:46:43
【问题描述】:

After running `RAILS_ENV=test rails test test/controllers我明白了

Expected response to be a &lt;2XX: success&gt;, but was a &lt;302: Found&gt;redirect to &lt;http://www.example.com/users/sign_in&gt;

回复正文:你是redirected

=======================


class OpeningsControllerTest < ActionDispatch::IntegrationTest

  test "should get new" do
    get new_opening_url
    assert_response :success
  end
end**

【问题讨论】:

    标签: ruby-on-rails session devise minitest


    【解决方案1】:

    你可以在运行测试用例时设置 current_user 吗?如果您没有分配当前用户,则在测试用例中的所有操作之前使用this 引用来分配当前用户。

    因为如果您在应用程序控制器中使用经过身份验证的用户或使用设计,则需要此方法来验证用户或管理员用户,如果未找到当前用户,则重定向到 user_sign_in_path。

    【讨论】:

    • 是的,我已经在 test_helper.rb 中设置了 current_user 我仍然收到此错误,我想我需要先登录然后这将起作用,但我不知道该怎么做,你能帮忙吗我?
    【解决方案2】:

    尝试类似:

    `
    class OpeningsControllerTest < ActionDispatch::IntegrationTest
    
      test "should get new" do
        sign_in users(:rathod)
        get new_opening_url
        assert_response :success
      end
    end**
    `
    

    这意味着你需要在一个fixture文件中创建一个用户:users.yml

    rathod:
      email: "JEVBR@test.com"
      first_name: "rathod"
      last_name: "bar"
    

    猜你正在使用 Devise,所以你需要设备的测试助手来访问“sign_in”助手

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-07
      • 1970-01-01
      • 2011-04-14
      • 2020-11-15
      • 1970-01-01
      • 2021-07-18
      • 2021-05-25
      相关资源
      最近更新 更多