require 'rails_helper'
RSpec.describe "Users", type: :request do
describe "GET /users" do
it "works! (now write some real specs)" do
get users_path
expect(response).to have_http_status(200)
end
end
end
$ bundle exec rspec spec/requests/users_spec.rb
# 上記のコードを実行した結果↓
Users
GET /users
works! (now write some real specs) (FAILED - 1)
Failures:
1) Users GET /users works! (now write some real specs)
Failure/Error: @user = User.find(params[:id])
ActiveRecord::RecordNotFound:
Couldn't find User without an ID
# ./app/controllers/users_controller.rb:39:in `set_user'
# ./spec/requests/users_spec.rb:9:in `block (3 levels) in <main>'
Finished in 0.07114 seconds (files took 3.07 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/requests/users_spec.rb:7 # Users GET /users works! (now write some real specs)
このようなエラーがでた。
Rails.application.routes.draw do
resources :articles
resources :users
get 'home/index'
# For details on the DSL available within this file, see <https://guides.rubyonrails.org/routing.html>
end
class UsersController < ApplicationController
before_action :set_user, onry: [:show, :update, :destroy]
def index
@users = User.all
render json: @users
end
def show
end
def create
#インスタンス DB に保存する
@user.save!
#json として値を渡す
render :show
end
def update
#探してきたレコードに対して変更を行う
@user.update!(user_params)
#jsonとして値を返す
render :show
end
def destroy
#探してきたレコードを削除する
@user.destroy
#jsonとして値を返す
render :show
end
private
def user_params
params.require(:user).permit(:name, :account, :email)
end
def set_user
binding.pry
@user = User.find(params[:id])
binding.pry
end
end
色々試したが解決に至らなかったので質問をしたところ...
まず、問題の切り分けをしなきゃいけなかったので