Walls hit by Rspec for the first time

Introduction

I finally decided to start working on Rspec. It seems that you can do various tests using this. It's amazing. However, it seemed a little tough, so I decided to use online teaching materials. This is Take off Rails.

https://freelance.cat-algorithm.com/lp/take-off-rails

I haven't had enough time these days, so I needed to streamline things. It's a form of buying time with money, which was very good. The information is aggregated and I asked if I didn't understand.

Even so, I've had a lot of trouble, so I'll write it here.

rails g model user -s can't

This time, to create spec / models / user_spec.rb and spec / factories / users.rb for the existing user model,

$ rails g model user -s

Tried to run. However, an error occurred.

$ rails g model user -s
Running via Spring preloader in process 1605
      invoke  active_record
The name 'User' is either already used in your application or reserved by Ruby on Rails. 
Please choose an alternative or use --force to skip this check and run this generator again.

Apparently the skip option wasn't available. I asked a question, committed all the modified files, then did $ rails g model user --force and ran $ git restore .. By doing this, the same effect as skip was obtained.

Error suffering from devise and normal action URL

When I tried to test the create action of the user controller, I got the following error.

Failures:
  1) Users POST /users Can create user records
     Failure/Error: expect{subject }.to change { User.count }.by(1)
       expected `User.count` to have changed by 1, but was changed by 0
     # ./spec/requests/users_spec.rb:53:in `block (3 levels) in <main>'

When I searched for the cause, I found that it did not transition to the user # create action. Instead, devise's #create action was being executed.

When I checked with $ rails routes, the paths of devise # controller and user # controller were the same. I have to change this a little.

It was set as follows.

config/routes.rb


Rails.application.routes.draw do
  root to: 'users#index'
  devise_for :users
  resources :users ,path_names: { create: 'make' } do
    resources :plants do
      resources :growth_records, shallow: true
    end
  end
  
  scope 'username' do
    resources :users, only: [:create]
  end

end

This time, I changed the part of scope. This eliminates routing conflicts. Part of the test looks like this:

RSpec.describe "Users", type: :request do
#Omission-----
  describe "POST /users" do
    subject { post('/username/users' ,params: params) }
    let(:params){ {user: attributes_for(:user)} }

    it "Can create user records" do
      expect{subject }.to change { User.count }.by(1)
      expect(response).to have_http_status(204)
    end
  end
#Omission-----
end

The previous post (users_path, params: params) has been changed to post ('/ username / users', params: params).

end

The world of Rspec is deep. Right now, I'm only conscious of no errors, but I want to think about "what tests are needed and how much" by myself.

After all, programming is fun.

Recommended Posts

Walls hit by Rspec for the first time
Spring Boot for the first time
Spring AOP for the first time
Introduction to java for the first time # 2
Learning for the first time java [Introduction]
I tried using Docker for the first time
Android Studio development for the first time (for beginners)
I tried touching Docker for the first time
Learn for the first time java # 3 expressions and operators
Oreore certificate https (2020/12/19) for the first time with nginx
Learning memo when learning Java for the first time (personal learning memo)
How to study kotlin for the first time ~ Part 2 ~
How to study kotlin for the first time ~ Part 1 ~
[Rails] I tried using the button_to method for the first time
Modeling a Digimon with DDD for the first time Part 1
Think when Rails (turbolinks) doesn't load the page for the first time
Touching kotlin for the first time-Enum Classes
[First Java] Make something that works with Intellij for the time being
The story of releasing the Android app to the Play Store for the first time.
[Socket communication (Java)] Impressions of implementing Socket communication in practice for the first time
Creating an app and deploying it for the first time on heroku
Programming for the first time in my life Java 1st Hello World
Use the l method for time notation
The story of intentionally using try catch for the first time in my life
Impressions and doubts about using java for the first time in Android Studio
A story about a super beginner participating in the AtCoder contest for the first time (AtCoder Beginner Contest 140)
Format the date and time given by created_at
Install Amazon Corretto (preview) for the time being
Use Java external library for the time being
Run Dataflow, Java, streaming for the time being
[CircleCI] I will explain the stupid configuration file (config.yml) that I wrote for the first time.
[Android studio / Java] What you don't understand when you touch it for the first time
Get the anime name for this term by scraping
Try running Spring Cloud Config for the time being
[First post] Output the characters Sold Out for sold products
Credentials referenced by the AWS SDK for Java by default
Command to try using Docker for the time being
A summary of what Java programmers find when reading Kotlin source for the first time