Rspec. Is 302 a success? Why 302?

189 views
Skip to first unread message

Ralph Shnelvar

unread,
Jul 14, 2017, 1:02:38 PM7/14/17
to Ruby on Rails: Talk
I have the following code, which was automagically created, in ~/controllers/articles_controller_spec.rb
  describe "GET #new" do
    it
"returns a success response" do
     
# See Hassan's answer in
     
#   https://groups.google.com/forum/#!topic/rubyonrails-talk/OQIYz9mnBXU
      byebug
     
get :new, params: {}, session: valid_session
      byebug
      expect
(response).to be_success
   
end
 
end

I see the following in STDOUT
something@something:~$ date
Thu Jul 13 15:14:33 MDT 2017
something@something
:~$ rspec

. . .

[72, 81] in /home/real-estate-data-mining/spec/controllers/articles_controller_spec.rb
   
72:       # get :new # , params: {}, session: valid_session
   
73:       byebug
   
74:       get :new, params: {}, session: valid_session
   
75:       # get :new, params: {}, session: {valid_session}
   
76:       byebug
=> 77:       expect(response).to be_success
   
78:     end
   
79:   end
   
80:   end
   
81:
(byebug) response.status
302

Why am I getting a 302 rather than a 200?

Question 2: Shouldn't 302 be a type of success?




Frederick Cheung

unread,
Jul 15, 2017, 3:30:05 PM7/15/17
to Ruby on Rails: Talk
Something in your controller code is redirecting - can't really say without seeing that code

> Question 2: Shouldn't 302 be a type of success?

The http standard places responses into 5 groups:
- informational (100)
- success (200)
- redirections (300)
- client error (400)
- server error (500)

So a 302 response is not a successful response by definition (of course from a user point of view this is not always the case: for validation errors rails often renders an error page (so a 200 response) whereas a successful create/edit is normally a redirect.

See https://developer.mozilla.org/en-US/docs/Web/HTTP/Status for example.

Fred
Reply all
Reply to author
Forward
0 new messages