What to do if you get a "302" error in your controller unit test code in Rails

Introduction

It's not a continuation of the previous article, but it has similar content. Also, it is only ** 1 example ** in the error of "302", so thank you.

error contents

Then, first of all, the following error </ font> appeared when implementing the unit test code of the controller. Please look

Terminal



 % bundle exec rspec spec/requests/orders_spec.rb

OrdersController
  GET /index
When I request the index action, the response is returned normally.(FAILED - 1)
When you request the index action, there is a description of the listed item in the response(FAILED - 2)


Failures:

  1) OrdersController GET /When you request the index index action, the response is returned normally.
     Failure/Error: expect(response.status).to eq 200
     
       expected: 200
            got: 302
     
       (compared using ==)
     # ./spec/requests/orders_spec.rb:24:in `block (3 levels) in <top (required)>'

  2) OrdersController GET /When you request the index index action, the response contains a description of the item that has already been listed.
     Failure/Error: expect(response.body).to include @item.details
       expected "<html><body>You are being <a href=\"http://www.example.com/users/sign_in\">redirected</a>.</body></html>" to include "Product description"
     # ./spec/requests/orders_spec.rb:29:in `block (3 levels) in <top (required)>'

~ Omitted ~

Verification of error content

Now, I would like to verify the content of the error. There are two </ font> in the relevant part that causes this error. First, first </ font> is the following part.

Terminal



  1)   Failure/Error: expect(response.status).to eq 200
     
       expected: 200
            got: 302

Next, second </ font> is the following part.

Terminal


  2)   Failure/Error: expect(response.body).to include @item.details

        expected "<html><body>You are being <a href=\"http://www.example.com/users/sign_in\">redirected</a>.</body></html>" to include "Product description"

Verification of error content

Now let's verify the error content.

--First, first error </ font> is expected and expects ** "200" (success) **, but the actual response returned is ** "302". It is (Found) **. Here is a detailed explanation of ** "302" (Found) **.

What is a 302 (Found) error?

** "The HyperText Transfer Protocol (HTTP) 302 Found redirect status response code indicates that the requested resource has been temporarily redirected to the URL indicated by Location, although the browser redirects to this page. , The search engine does not update the link to the resource. "**

To briefly explain the above, the content is "You were redirected to a different page instead of the page you originally wanted to transition to."

** (Reference site quoted) ** Reference site for HTTP response status code


--Then, next is second error </ font>, but the point I focused on is the following part.


<a href=\"http://www.example.com/users/sign_in\">redirected</a>.

First of all, there is ** "users / sign_in" **, so it can be inferred that the ** login screen ** is involved. And then there is ** "redirected" **, so you can guess that it was ** redirected ** here as well.

Inferring from the above verification contents, it can be assumed that ** "Is it skipped to the login screen by redirect?" **.

Then, when it comes to the part where such processing is performed, it turns out that the part ** "authenticate_user!" ** corresponds. Here's a little ** "authenticate_user!" **.

What is authenticate_user!

If the user is not logged in when the process is executed, the user is transitioned to the login screen. It is a method for devise that does the processing.

From the above points, I decided to comment out the relevant ** "authenticate_user!" ** when executing the test code.

orders_controller.rb


class OrdersController < ApplicationController
  before_action :authenticate_user!(Comment out this line)

Run the unit test code again

Terminal


% bundle exec rspec spec/requests/orders_spec.rb

OrdersController
  GET /index
When I request the index action, the response is returned normally.
When you request the index action, there is a description of the listed item in the response
When you request the index action, there is an image of the listed item in the response
When you request the index action, the selling price of the listed item exists in the response
When you request the index action, there is a wording to confirm the purchase contents in the response

Finished in 5.79 seconds (files took 10.02 seconds to load)
5 examples, 0 failures

As mentioned above, the test code was successfully executed. </ font>

in conclusion

This ** error (302) ** was an error that occurred because ** redirected ** to the login screen with ** "authenticate_user!" ** when executing the controller test code. The process of logging in with the integration test code is easy, but I was not sure if I could log in with the unit test code of the controller, so I dealt with it by commenting out this time.

The site that I referred to in this article

Reference site for HTTP response status code

Recommended Posts