Search code examples
ruby-on-railsrspecrspec-rails

Expected the response to have status code 200 but it was 302 in requests test in rspec


I found lots of similar questions posted by others but none of the solutions worked.

My /spec/requests/questions_spec.rb is

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

Now my rspec error

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)>'

Can anyone help me? How do I customize my code to get the status code 200 ?


Solution

  • 302 - Found. I think you are using rails scaffold. Scaffold syntax return 302 status on get method. If you need 200. Youcan customize your code.