ログインしている本人のみ自分の情報を編集できるように設定したいのですが、Rspecでエラーが発生します。
他人の編集画面に移ろうとしたときはuser/idに遷移させたいです。
こちらのエラー文をもとに修正したいのですが、方法が分からないためアドバイス頂けると嬉しいです。
よろしくお願い致します。
ターミナル 3) ユーザーのテスト 編集のテスト 他人の編集画面への遷移 遷移できない Failure/Error: expect(current_path).to eq('/users/' + user.id.to_s) expected: "/users/2" got: "/users/1" (compared using ==)
usersコントローラ class UsersController < ApplicationController before_action :authenticate_user! before_action :correct_user, only: [:edit, :update] def index @book = Book.new @users = User.page(params[:page]).reverse_order end def show @user = User.find(params[:id]) @book = Book.new @books = @user.books.page(params[:page]).reverse_order end def edit @user = User.find(params[:id]) end def update @user = User.find(params[:id]) if @user.update(user_params) flash[:notice] = "successfully." redirect_to user_path(@user.id) else render :edit end end private def user_params params.require(:user).permit(:name, :introduction, :profile_image) end def correct_user @user = User.find(params[:id]) redirect_to(user_url) unless @user == current_user end end
rake routes ~省略~ edit_user_registration GET /users/edit(.:format) users/registrations#edit user_registration PATCH /users(.:format) users/registrations#update PUT /users(.:format) users/registrations#update DELETE /users(.:format) users/registrations#destroy POST /users(.:format) users/registrations#create root GET / top#top home_about GET /home/about(.:format) top#home users GET /users(.:format) users#index POST /users(.:format) users#create new_user GET /users/new(.:format) users#new edit_user GET /users/:id/edit(.:format) users#edit user GET /users/:id(.:format) users#show PATCH /users/:id(.:format) users#update PUT /users/:id(.:format) users#update DELETE /users/:id(.:format) users#destroy books GET /books(.:format) books#index POST /books(.:format) books#create new_book GET /books/new(.:format) books#new edit_book GET /books/:id/edit(.:format) books#edit book GET /books/:id(.:format) books#show PATCH /books/:id(.:format) books#update PUT /books/:id(.:format) books#update DELETE /books/:id(.:format) books#destroy
【追記】
system/users_spec.rb describe '編集のテスト' do context '自分の編集画面への遷移' do it '遷移できる' do visit edit_user_path(user) expect(current_path).to eq('/users/' + user.id.to_s + '/edit') end end context '他人の編集画面への遷移' do it '遷移できない' do visit edit_user_path(test_user2) expect(current_path).to eq('/users/' + user.id.to_s) end end context '表示の確認' do before do visit edit_user_path(user) end it 'User infoと表示される' do expect(page).to have_content('User info') end it '名前編集フォームに自分の名前が表示される' do expect(page).to have_field 'user[name]', with: user.name end it '画像編集フォームが表示される' do expect(page).to have_field 'user[profile_image]' end it '自己紹介編集フォームに自分の自己紹介が表示される' do expect(page).to have_field 'user[introduction]', with: user.introduction end it '編集に成功する' do click_button 'Update User' expect(page).to have_content 'successfully' expect(current_path).to eq('/users/' + user.id.to_s) end it '編集に失敗する' do fill_in 'user[name]', with: '' click_button 'Update User' expect(page).to have_content 'error' #もう少し詳細にエラー文出したい expect(current_path).to eq('/users/' + user.id.to_s) end end end
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2020/02/28 01:37 編集
2020/02/28 01:42 編集
退会済みユーザー
2020/02/28 01:43
2020/02/28 01:44
退会済みユーザー
2020/02/28 02:10
2020/02/28 02:17
退会済みユーザー
2020/02/28 02:41
2020/02/28 03:55
退会済みユーザー
2020/02/28 04:58
2020/02/28 06:27
退会済みユーザー
2020/02/28 06:50 編集
2020/02/28 06:54
退会済みユーザー
2020/02/28 07:02
退会済みユーザー
2020/02/28 12:07