【问题标题】:Expected the response to have status code 200 but it was 302 in requests test in rspec预计响应的状态代码为 200,但在 rspec 中的请求测试中为 302
【发布时间】:2018-06-26 07:09:40
【问题描述】:

我发现其他人发布了很多类似的问题,但没有一个解决方案有效。

我的 /spec/requests/questions_spec.rb 是

require 'rails_helper'
RSpec.describe "Questions", type: :request do
  describe "GET /questions" do
    it "works! (now write some real specs)" do
      get questions_path
      expect(response).to have_http_status(200)
    end
  end
end

现在我的 rspec 错误

Questions GET /questions works! (now write some real specs)
 Failure/Error: expect(response).to have_http_status(200)
   expected the response to have status code 200 but it was 302
 # ./spec/requests/questions_spec.rb:7:in `block (3 levels) in <top (required)>'

谁能帮帮我?如何自定义代码以获取状态码 200?

【问题讨论】:

  • 200 成功,302 找到。 Rails 脚手架在 get 方法上返回 302。
  • 你能在这里分享你的控制器代码吗?
  • 也许您的应用中有身份验证,这就是请求返回 302 的原因
  • 您是否为此控制器使用身份验证? @Sridhar

标签: ruby-on-rails rspec rspec-rails


【解决方案1】:

302 - Found。我认为您正在使用rails脚手架。支架语法在 get 方法上返回 302 状态。如果你需要 200。你可以自定义你的代码。

【讨论】:

  • 感谢 Velu Dhanesh。如何自定义状态码为 200?
  • 更改您的回复,例如render json: {data: 'your variable', status: 200, message: 'success'}
【解决方案2】:

【讨论】:

    【解决方案3】:

    当我正确处理这段代码时,我遇到了类似expected the response to have a success status code (2xx) but it was 302 的问题

    描述“GET #index”做

      it "returns http success" do
        get :index
        expect(response).to have_http_status(:success)
      end
    end
    

    你知道吗:我没有使用devise gem,所以我不能使用login_user,不幸的是我忘记了如何在没有devise 的情况下实现登录。它以某种方式使用session 保持登录状态。所以我做了这样的事情,它奏效了:

    描述“GET #index”做

      it "returns http success" do
    
          admin = User.create(name:  "admin@gmail.com",
               email: "admin@gmail.com",
               password:              "admin@gmail.com",
               password_confirmation: "admin@gmail.com",
               admin:     true,
               activated: true,
               activated_at: Time.zone.now)
    
          session[:user_id] = admin.id
    
        get :index
        expect(response).to have_http_status(:success)
      end
    
    end
    

    【讨论】:

      【解决方案4】:

      您很可能使用@Velusamy Venkatraman 和@Raj 提到的脚手架生成的方法。所以,如果你想获得 2xx 状态,你需要sign_in,这意味着完全成功。 302 状态表示页面存在,但您被重定向(主要是在某些错误的情况下)。

      执行以下操作来解决问题:

      第 1 步: 您可以在 spec 文件夹中创建如下自定义方法,然后简单地使用它们:

      module ControllerMacros
        def login_admin
          before(:each) do
            @request.env["devise.mapping"] = Devise.mappings[:admin]
            sign_in FactoryBot.create(:user, admin:true)
          end
        end
      
        def login_user
          before(:each) do
            @request.env["devise.mapping"] = Devise.mappings[:user]
            sign_in FactoryBot.create(:user)
          end
        end
      end
      

      第 2 步:

      login_userlogin_admin 添加到您的规范文件中,只要您需要并进行更改

      let(:valid_session) { {} }

      let(:valid_session) { {"warden.user.user.key" =&gt; session["warden.user.user.key"]} }

      我希望您使用 devisewarden 如果您不想担心会话/登录/注册问题,它们真的很有用。

      您可以在此处查看他们的文档: plataformatec/devise wardencommunity/warden

      此答案是根据设计文档编写的: devise-wiki-how-to-test

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-10-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-11-26
        相关资源
        最近更新 更多