预期响应的状态代码为200,但RSpec中的请求测试中的状态代码为302

人气:49 发布:2023-01-03 标签: ruby-on-rails rspec rspec-rails

问题描述

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

我的/SPEC/REQUESTS/QUIES_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?

推荐答案

302 - Found。我认为您使用的是Rails脚手架。GET方法上的scaffold语法返回302状态。如果你需要200的话。您可以自定义代码。

14