I want to test the post request at the time of user registration with rspec.
The test code is as follows.

RSpec.describe'UsersRequests', type :: request do
  before do
    get signup_path
  describe'users #<create>' do
    it'post data can be received' do
      user = create (: user)
      post users_path, params: {user: user.id}
      expect (response) .to have_http_status (302)
FactoryBot.define do
  factory: user do
    name {Faker :: Name.name}
    sequence (: email) {| n | "example # {n} @ example.com"}
    image {'MyString'}
    comment {'MyText'}
    sex {'men'}
    password {'foobar'}
    password_confirmation {'foobar'}
    admin {false}

When you actually run the test

  users #<create>
    Being able to receive post data (FAILED -1)
  1) UsersRequests users #<create>post Data can be received
     Failure/Error: expect (response) .to have_http_status (302)
       expected the response to have status code 302 but it was 422
     # ./spec/requests/users_requests_390spec.rb:14: in `block (3 levels) in<top (required)>'
Finished in 0.89641 seconds (files took 1.54 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/requests/users_requests_spec.rb:11 #UserRequests users #<create>post Being able to receive data

The expected status code is 302, but the result is 422.
And that

As a result of examining status code 422, it may be a validation error.

class User

On the browser, it feels like it's working fine.
How do I test a post request and how to debug status code 422?

If i have any other data you need, please contact us.

that's all, thank you very much.

  • Answer # 1

    In this case, it is solved by adding the following to users_controller
    protect_from_forgery with:: exception, only:: create

    Errors occurring in relation to CSRF measures of rails
    I'm sorry I couldn't explain it well because I haven't investigated it in detail.

  • Answer # 2

    Isn't it because you're trying to save an image without using ImageUploader?