質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

RSpec

RSpecはRuby用のBDD(behaviour-driven development)フレームワークです。

Ruby on Rails 7

Ruby on Rails 7は、2021年12月に正式リリースされました。Ruby on Railsのバージョン7であり、フロントエンド開発環境を大幅に刷新。Node.jsを用いない構成がデフォルトになっています。

Q&A

解決済

1回答

398閲覧

system_specにてclick_buttonでRuntimeerror

.taniyan

総合スコア17

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

RSpec

RSpecはRuby用のBDD(behaviour-driven development)フレームワークです。

Ruby on Rails 7

Ruby on Rails 7は、2021年12月に正式リリースされました。Ruby on Railsのバージョン7であり、フロントエンド開発環境を大幅に刷新。Node.jsを用いない構成がデフォルトになっています。

0グッド

0クリップ

投稿2022/10/30 02:43

前提

deviseで新規登録後、プロフィール設定画面にリダイレクトするように設定

userモデルのカラムであるuser_nameを、空欄で更新ボタンを押すとバリデーションがかかり、登録できないようにしてあります。

実現したいこと

system_specにて「user_nameが空欄の時、登録できないこと」のテストが通るようにしたい

発生している問題・エラーメッセーじ

1) Users user CRUD ログイン前 新規登録 user_nameが空欄の時、登録できないこと Failure/Error: click_button '更新' RuntimeError: #<ActiveStorage::Attached::One:0x00007faa8dd5e960> file does not exist # /usr/local/bundle/gems/rack-test-2.0.2/lib/rack/test/uploaded_file.rb:95:in `initialize_from_file_path' # /usr/local/bundle/gems/rack-test-2.0.2/lib/rack/test/uploaded_file.rb:36:in `initialize' # /usr/local/bundle/gems/capybara-3.37.1/lib/capybara/rack_test/form.rb:105:in `new' # /usr/local/bundle/gems/capybara-3.37.1/lib/capybara/rack_test/form.rb:105:in `file_to_upload' # /usr/local/bundle/gems/capybara-3.37.1/lib/capybara/rack_test/form.rb:90:in `add_input_param' # /usr/local/bundle/gems/capybara-3.37.1/lib/capybara/rack_test/form.rb:33:in `block in params' # /usr/local/bundle/gems/capybara-3.37.1/lib/capybara/rack_test/form.rb:31:in `each' # /usr/local/bundle/gems/capybara-3.37.1/lib/capybara/rack_test/form.rb:31:in `each_with_object' # /usr/local/bundle/gems/capybara-3.37.1/lib/capybara/rack_test/form.rb:31:in `params' # /usr/local/bundle/gems/capybara-3.37.1/lib/capybara/rack_test/form.rb:43:in `submit' # /usr/local/bundle/gems/capybara-3.37.1/lib/capybara/rack_test/node.rb:75:in `click' # /usr/local/bundle/gems/capybara-3.37.1/lib/capybara/rack_test/node.rb:138:in `call' # /usr/local/bundle/gems/capybara-3.37.1/lib/capybara/rack_test/node.rb:138:in `click' # /usr/local/bundle/gems/capybara-3.37.1/lib/capybara/node/element.rb:172:in `block in click' # /usr/local/bundle/gems/capybara-3.37.1/lib/capybara/node/element.rb:608:in `block in perform_click_action' # /usr/local/bundle/gems/capybara-3.37.1/lib/capybara/node/base.rb:83:in `synchronize' # /usr/local/bundle/gems/capybara-3.37.1/lib/capybara/node/element.rb:608:in `perform_click_action' # /usr/local/bundle/gems/capybara-3.37.1/lib/capybara/node/element.rb:171:in `click' # /usr/local/bundle/gems/capybara-3.37.1/lib/capybara/node/actions.rb:58:in `click_button' # /usr/local/bundle/gems/capybara-3.37.1/lib/capybara/session.rb:771:in `click_button' # /usr/local/bundle/gems/capybara-3.37.1/lib/capybara/dsl.rb:52:in `call' # /usr/local/bundle/gems/capybara-3.37.1/lib/capybara/dsl.rb:52:in `click_button' # ./spec/system/users_spec.rb:71:in `block (5 levels) in <main>' Finished in 0.81642 seconds (files took 1.74 seconds to load) 22 examples, 1 failure Failed examples: rspec ./spec/system/users_spec.rb:64 # Users user CRUD ログイン前 新規登録 user_nameが空欄の時、登録できないこと

該当のソースコード

spec/system/users_spec.rb

1require 'rails_helper' 2 3RSpec.describe "Users", type: :system do 4 before do 5 driven_by(:rack_test) 6 end 7 8 let(:user) { create(:user) } 9 let(:other_user) { create(:user) } 10 11 describe 'user CRUD' do 12 describe 'ログイン前' do 13 describe '新規登録' do 14 context 'フォームの入力が正常' do 15 it 'ユーザーの新規登録が成功' do 16 visit new_user_registration_path 17 fill_in 'user[email]', with: 'a@example.com' 18 fill_in 'user[password]', with: 'password' 19 fill_in 'user[password_confirmation]', with: 'password' 20 click_button '登録する!' 21 expect(page).to have_text('プロフィール') 22 end 23 end 24 context 'メールアドレスが未記入' do 25 it 'ユーザーの登録が失敗' do 26 visit new_user_registration_path 27 fill_in 'user[email]', with: nil 28 fill_in 'user[password]', with: 'password' 29 fill_in 'user[password_confirmation]', with: 'password' 30 click_button '登録する!' 31 expect(page).to have_text('Welcome!') 32 end 33 end 34 context 'パスワードが未記入' do 35 it 'ユーザーの登録が失敗' do 36 visit new_user_registration_path 37 fill_in 'user[email]', with: 'a@example.com' 38 fill_in 'user[password]', with: nil 39 fill_in 'user[password_confirmation]', with: nil 40 click_button '登録する!' 41 expect(page).to have_text('Welcome!') 42 end 43 end 44 context '確認パスワードがパスワードと不一致' do 45 it 'ユーザーの登録が失敗' do 46 visit new_user_registration_path 47 fill_in 'user[email]', with: 'a@example.com' 48 fill_in 'user[password]', with: 'password1' 49 fill_in 'user[password_confirmation]', with: 'password2' 50 click_button '登録する!' 51 expect(page).to have_text('Welcome!') 52 end 53 end 54 context 'パスワードが6文字未満' do 55 it 'ユーザーの登録が失敗' do 56 visit new_user_registration_path 57 fill_in 'user[email]', with: 'a@example.com' 58 fill_in 'user[password]', with: 'pass' 59 fill_in 'user[password_confirmation]', with: 'pass' 60 click_button '登録する!' 61 expect(page).to have_text('Welcome!') 62 end 63 end 64 it 'user_nameが空欄の時、登録できないこと' do <-----------------問題のコード 65 visit new_user_registration_path 66 fill_in 'user[email]', with: 'a@example.com' 67 fill_in 'user[password]', with: 'password' 68 fill_in 'user[password_confirmation]', with: 'password' 69 click_button '登録する!' 70 fill_in 'user_name', with: nil 71 click_button '更新' 72 expect(page).to have_text('ユーザー名が入力されていません') 73 end 74 end 75 76 it 'サービス申し込みができないこと' do 77 visit services_path(user.id) 78 expect(page).to have_no_text('申し込む') 79 end 80 81 end 82 describe 'ログイン後' do 83 it 'ログアウトができること' do 84 visit new_user_registration_path 85 fill_in 'user[email]', with: 'a@example.com' 86 fill_in 'user[password]', with: 'password' 87 fill_in 'user[password_confirmation]', with: 'password' 88 click_button '登録する!' 89 click_button 'ログアウト' 90 expect(page).to have_text('響け、私のギター。') 91 end 92 end 93 end 94end

spec/factories/users.rb

1FactoryBot.define do 2 factory :user do 3 sequence(:email) { |n| "person#{n}@example.com" } 4 password { "123456" } 5 user_name { "test" } 6 self_introduce { "test" } 7 end 8end 9

app/views/home/profile.html.erb

1<div class="profile"> 2 <div class="profile-form"> 3 <div class="text-center"> 4 <h4>プロフィール</h4> 5 <% if @user.profile_image.present? %> 6 <%= image_tag @user.profile_image.url, class: "rounded img-fluid" %> 7 <% else %> 8 <img class="login-icon" src="/assets/default_icon-9263fc59c414b7228d256fc178dcb22183561357950a68f5ad086ba7ee054974.jpeg"> 9 <% end %> 10 </div> 11 <div class="profile-container"> 12 <%= form_with url: update_home_path, method: :post do |f| %> 13 <%= render "shared/partial", obj:@user %> 14 <table> 15 <tr> 16 <th><%= f.label :profile_image, "アイコン画像" , class:"form-label" %></th> 17 <td><%= f.file_field :profile_image, class: "mb-3 form-control", value: @user.profile_image %></td> 18 </tr> 19 <tr> 20 <th><%= f.label :user_name ,"名前", class: "form-label" %></th> 21 <td><%= f.text_field :user_name, class: "mb-3 form-control", value: @user.user_name %></td> 22 </tr> 23 <tr> 24 <th><%= f.label :self_introduce ,"自己紹介", class: "form-label" %></th> 25 <td><%= f.text_area :self_introduce, class: "mb-3 form-control", value: @user.self_introduce %></td> 26 </tr> 27 <tr> 28 <th><%= f.label :youtube_url ,"youtubeチャンネルのurl", class: "form-label" %></th> 29 <td><%= f.text_area :youtube_url, class: "mb-3 form-control", value: @user.youtube_url %></td> 30 </tr> 31 <tr> 32 <th><%= f.label :instagram_url ,"instagramのurl", class: "form-label" %></th> 33 <td><%= f.text_area :instagram_url, class: "mb-3 form-control", value: @user.instagram_url %></td> 34 </tr> 35 <tr> 36 <th><%= f.label :twitter_url ,"Twwiterのurl", class: "form-label" %></th> 37 <td><%= f.text_area :twitter_url, class: "mb-3 form-control", value: @user.twitter_url %></td> 38 </tr> 39 </table> 40 <ul class="mb-3 form-check"> 41 <li><%= f.submit "更新", class: "btn btn-primary"%></li>  <-----------更新と確かに記載している 42 </ul> 43 <% end %> 44 </div> 45 </div> 46</div> 47

app/models/user.rb

1class User < ApplicationRecord 2 # Include default devise modules. Others available are: 3 # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable 4 devise :database_authenticatable, :registerable, 5 :recoverable, :rememberable, :validatable 6 7 has_one_attached :profile_image 8 9 has_many :services, dependent: :destroy 10 has_many :movies, dependent: :destroy 11 12 validates :user_name, presence: true, on: :update 13end 14

わからない点

profile画面では確かに「更新」ボタンがあるのに、なぜ Failure/Error: click_button '更新'となるのかが
わかりません。

補足情報(FW/ツールのバージョンなど)

rails 7

ruby3.1.1

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

自己解決

profile.html.erb画面の画像を選択するフォームのvalue: @user.profile_imagevalue: @user.profile_image.urlをしたところ、上手くテストが通りました。

投稿2022/11/02 10:40

.taniyan

総合スコア17

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問