requst specのupdateの書き方を知りたい。
gem deviseを使用しています。
実際のコード
ruby
1 def update 2 current_user.update(update_params) 3 redirect_to root_path 4 rescue StandardError 5 redirect_to root_path 6 end 7 8 private 9 def update_params 10 params.require(:user).permit(:lastname, :firstname, :tell, :sex, :birth_date, :user_type_id, :image) 11 end 12
ruby
1FactoryBot.define do 2 factory :user do 3 email { 'hoge@mail.com'} 4 password { 'password' } 5 password_confirmation { 'password' } 6 accepted { true } 7 promotion_code { '12345' } 8 confirmed_at { Date.today } 9 end 10end 11
ruby
1requests/users_spec.rb 2RSpec.describe "Users", type: :request do 3 let(:user) { create(:user, id: 1) } 4 5 6 describe "PATCH / user_path" do 7 context 'パラメータで正の時' do 8 let(:params_success) { { user: { lastname: 'last_name', firstname: 'first_name', tell: '09099998888', sex: '男性', birth_date: '2000-02-21', user_type_id: 1, image: '' } } } 9 10 it 'リクエストが成功すること' do 11 patch '/users/' + user.id.to_s, params: params_success 12 expect(User.find_by(id: user.id).firstname).to eq 'first_name' 13 end 14 end 15 end 16end
結果>
- expected: first_name
- got: nil
困っていること
patchでレコードを更新しているはずなのに、中身が空のままでテストが落ちてしまう。
test.log
1 2 [1m[35m (0.4ms)[0m [1m[35mSET NAMES utf8 COLLATE utf8_general_ci, @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'), @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483[0m 3 [1m[35m (0.4ms)[0m [1m[34mSELECT `ar_internal_metadata`.`value` FROM `ar_internal_metadata` WHERE `ar_internal_metadata`.`key` = 'schema_sha1'[0m 4 [1m[35m (0.3ms)[0m [1m[35mSET NAMES utf8 COLLATE utf8_general_ci, @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'), @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483[0m 5 [1m[35m (0.3ms)[0m [1m[34mSELECT `schema_migrations`.`version` FROM `schema_migrations` ORDER BY `schema_migrations`.`version` ASC[0m 6 [1m[35m (0.4ms)[0m [1m[35mBEGIN[0m 7 [1m[35m (0.4ms)[0m [1m[35mSAVEPOINT active_record_1[0m 8 [1m[36mUser Exists? (0.4ms)[0m [1m[34mSELECT 1 AS one FROM `users` WHERE `users`.`email` = BINARY 'hoge@mail.com' LIMIT 1[0m 9 [1m[36mUser Create (0.7ms)[0m [1m[32mINSERT INTO `users` (`id`, `email`, `encrypted_password`, `confirmed_at`, `created_at`, `updated_at`, `accepted`, `promotion_code`) VALUES (1, 'hoge@mail.com', '$2a$04$4bYXcGJzKQDHBEvXzzzbSulv3URSCxygq6dJNtTshhAY199MNvFG6', '2019-10-13 00:00:00', '2019-10-13 10:57:06.721754', '2019-10-13 10:57:06.721754', TRUE, '12345')[0m 10 [1m[35m (0.4ms)[0m [1m[35mRELEASE SAVEPOINT active_record_1[0m 11Started PATCH "/users/1" for 127.0.0.1 at 2019-10-13 10:57:06 +0000 12 [1m[35m (0.5ms)[0m [1m[35mSAVEPOINT active_record_1[0m 13 [1m[36mUser Update (0.5ms)[0m [1m[33mUPDATE `users` SET `users`.`sign_in_count` = 1, `users`.`current_sign_in_at` = '2019-10-13 10:57:06', `users`.`last_sign_in_at` = '2019-10-13 10:57:06', `users`.`current_sign_in_ip` = '127.0.0.1', `users`.`last_sign_in_ip` = '127.0.0.1', `users`.`updated_at` = '2019-10-13 10:57:06.738645' WHERE `users`.`id` = 1[0m 14 [1m[35m (0.3ms)[0m [1m[35mRELEASE SAVEPOINT active_record_1[0m 15Processing by UsersController#update as HTML 16 Parameters: {"user"=>{"id"=>"1", "lastname"=>"last_name", "firstname"=>"first_name", "tell"=>"09088997766", "sex"=>"男性", "birth_date"=>"1996-04-21", "user_type_id"=>"1", "image"=>""}, "id"=>"1"} 17 [1m[36mUser Load (0.6ms)[0m [1m[34mSELECT `users`.* FROM `users` WHERE `users`.`id` = 1 LIMIT 1[0m 18[31mUnpermitted parameter: :id[0m 19 [1m[35m (0.3ms)[0m [1m[35mSAVEPOINT active_record_1[0m 20 [1m[36mUser Update (0.4ms)[0m [1m[33mUPDATE `users` SET `users`.`updated_at` = '2019-10-13 10:57:06.770118', `users`.`firstname` = 'first_name', `users`.`lastname` = 'last_name', `users`.`tell` = '09088997766', `users`.`sex` = '男性', `users`.`birth_date` = '1996-04-21', `users`.`user_type_id` = 1 WHERE `users`.`id` = 1[0m 21 [1m[36mActiveStorage::Blob Load (0.5ms)[0m [1m[34mSELECT `active_storage_blobs`.* FROM `active_storage_blobs` INNER JOIN `active_storage_attachments` ON `active_storage_blobs`.`id` = `active_storage_attachments`.`blob_id` WHERE `active_storage_attachments`.`record_id` = 1 AND `active_storage_attachments`.`record_type` = 'User' AND `active_storage_attachments`.`name` = 'image' LIMIT 1[0m 22 [1m[35m (0.4ms)[0m [1m[31mROLLBACK TO SAVEPOINT active_record_1[0m 23Redirected to http://www.example.com/users/1/profile 24Completed 302 Found in 31ms (ActiveRecord: 4.1ms | Allocations: 15462) 25 [1m[35m (1.1ms)[0m [1m[31mROLLBACK[0m
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/10/13 11:00
2019/10/13 13:23