Ruby on Rails Saturday, July 15, 2017

On Friday, July 14, 2017 at 6:02:38 PM UTC+1, Ralph Shnelvar wrote:
> 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?
>
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

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/b6c714ec-aff2-44f6-97c7-6b25a006ebad%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment