Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 986
Sridhar Avatar asked Jun 26 '18 07:06

Sridhar


People also ask

What does a 200 status code mean?

A 200 status code is the most popular code sent by servers on the Internet. If a site sends this response, it means the response is a success. For example, let’s assume that you landed on this very page. Our server responded with an HTTP status code 200. The server found the resource (this page), and everything was a success.

What are HTTP status codes and why are they important?

Status codes let us know whether the HTTP request was a success, a failure, or something in between. Some status codes are more common than others. For example, when you're doing digital marketing, you'll often come across status code 200, status code 301 and status code 404 - but you may never see status code 206 or 307.

How many requests should you see on your website?

You would usually only see this response when you're using a digital marketing tool that shows you the status code specifically. In general, you want to see 200 requests. They are good! Let's talk about how the HTTP protocol works. At its very foundation, the Internet is made up of two core things: clients and servers.

What does it mean when my HTTP status code is “unavailable”?

That means something went wrong with the response (website/server) and not the request (client/user). They include: Looking for more on a particular status code? We have a series of short guides on every HTTP response, so you can optimize your digital marketing strategy.


2 Answers

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.

like image 156
Velusamy Venkatraman Avatar answered Sep 24 '22 10:09

Velusamy Venkatraman


I think you are missing authentication.

use login_user

https://github.com/heartcombo/devise/wiki/How-To:-Test-controllers-with-Rails-(and-RSpec)

like image 23
Raj Avatar answered Sep 23 '22 10:09

Raj