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

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

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

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

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

1418閲覧

spec 結合テストについて

tomsuma

総合スコア38

Ruby

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

Ruby on Rails 6

Ruby on Rails 6は、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2020/08/29 22:43

編集2020/08/30 00:40

spec system結合テストにてバリデーションにユーザーを入れてとの指示が消えません
beforeのところに
@user = FactoryBot.create(:user)

を入れてもダメでした、、

エラー文 9) 本詳細 ログインしていない状態で本詳細ページに遷移できるもののコメント投稿欄が表示されない Failure/Error: @book = FactoryBot.create(:book) ActiveRecord::RecordInvalid: バリデーションに失敗しました: Userを入力してください
テスト RSpec.describe '本編集', type: :system do before do @book1 = FactoryBot.create(:book) @book2 = FactoryBot.create(:book) end context '本編集ができるとき' do it 'ログインしたユーザーは自分が投稿した本の編集ができる' do # 本1を投稿したユーザーでログインする visit new_user_session_path fill_in 'user_email', with: @user.email fill_in 'user_password', with: @user.password find('input[name="commit"]').click expect(current_path).to eq root_path # 編集ページへ遷移する visit edit_book_path(@book) # 投稿内容を編集する fill_in 'book_image', with: "#{@book.image}+編集した画像URL" fill_in 'book_text', with: "#{@book.text}+編集したテキスト" # 編集してもbookモデルのカウントは変わらない expect{ find('input[name="commit"]').click }.to change { book.count }.by(0) # 編集完了画面に遷移する expect(current_path).to eq book_path(@book) # 「更新が完了しました」の文字がある expect(page).to have_content('更新が完了しました。') # トップページに遷移する visit root_path # トップページには先ほど変更した内容の本が存在する(画像) expect(page).to have_selector ".content_post[style='background-image: url(#{@book.image}+編集した画像URL);']" # トップページには先ほど変更した内容の本が存在する(テキスト) expect(page).to have_content("#{@book.text}+編集したテキスト") end end
require 'rails_helper' RSpec.describe 'ユーザー新規登録', type: :system do before do @user = FactoryBot.build(:user) end context 'ユーザー新規登録ができるとき' do it '正しい情報を入力すればユーザー新規登録ができてトップページに移動する' do # トップページに移動する visit root_path # 新規登録ページへ移動する visit new_user_registration_path # ユーザー情報を入力する fill_in 'user_nickname', with: @user.nickname fill_in 'user_email', with: @user.email fill_in 'user_password', with: @user.password # トップページへ遷移することを確認する expect(current_path).to eq root_path # カーソルを合わせるとログアウトボタンが表示されることを確認する expect( find(".logout") ).to have_content('ログアウト') # サインアップページへ遷移するボタンやログインページへ遷移するボタンが表示されていないことを確認する expect(page).to have_no_content('新規登録') expect(page).to have_no_content('ログイン') end end context 'ユーザー新規登録ができないとき' do it '誤った情報ではユーザー新規登録ができずに新規登録ページへ戻ってくる' do # トップページに移動する visit root_path # 新規登録ページへ移動する visit new_user_registration_path # ユーザー情報を入力する fill_in 'user_nickname', with: "" fill_in 'user_email', with: "" fill_in 'user_password', with: "" # サインアップボタンを押してもユーザーモデルのカウントは上がらないことを確認する expect{ find('input[name="commit"]').click }.to change { User.count }.by(0) # 新規登録ページへ戻されることを確認する expect(current_path).to eq "/users" end end end RSpec.describe 'ログイン', type: :system do before do @user = FactoryBot.create(:user) end context 'ログインができるとき' do it '保存されているユーザーの情報と合致すればログインができる' do # トップページに移動する visit root_path # トップページにログインページへ遷移するボタンがあることを確認する expect(page).to have_content('ログイン') # ログインページへ遷移する visit new_user_session_path # 正しいユーザー情報を入力する fill_in 'user_email', with: @user.email fill_in 'user_password', with: @user.password # ログインボタンを押す find('input[name="commit"]').click # トップページへ遷移することを確認する expect(current_path).to eq root_path end end context 'ログインができないとき' do it '保存されているユーザーの情報と合致しないとログインができない' do # トップページに移動する visit root_path # トップページにログインページへ遷移するボタンがあることを確認する expect(page).to have_content('ログイン') # ログインページへ遷移する visit new_user_session_path # ユーザー情報を入力する fill_in 'user_email', with: "" fill_in 'user_password', with: "" # ログインボタンを押す find('input[name="commit"]').click # ログインページへ戻されることを確認する expect(current_path).to eq new_user_session_path end end end

本編集画面

<body><div class="container"> <div class="sec1title"> <h2>*本について編集を加える!*</h2> </div> <%= form_for (@book) do |f| %> <div class="tag-field", id='tag-field'> <%= f.label :tag_name, "タグ" %> <%= f.text_field :tag_name, placeholder: "検索されやすいキーワードを入れましょう" %> </div> <div class="field" type="text"> <%= f.label :name, "本の名前" %><br /> <%= f.text_field :name %> </div> <div class="genre" > <%= f.label :genre_id, "本のジャンル" %><br /> <%= f.collection_select(:genre_id,Genre.all,:id,:type,{prompt: "---"},{class:"select-box"}) %> </div> <div class="form-input"> <div class="text-image-content"> <%= f.label :content, "説明文" %><br /> <%= f.text_area :content, placeholder: "話したい内容について、ご自由にお書きください" , rows: "12" %> </div> <br /><br /> <label class="form-image"> <span class="image-file">画像</span> <%= f.file_field :image %> </label> <div class="image-stage", id="image-list"></div> </div> <div class="actions"> <%= f.submit "更新", class: "btn1" %> </div> <% end %> <%# ホームボタン %> <%= render "shared/sidebar" %> </div> </body>
books.rb FactoryBot.define do factory :book do name { '人間失格' } content { '紹介文' } genre_id { '5' } end end
user.rb FactoryBot.define do factory :user do nickname { 'suzuki' } email { 'example@cmail.com' } password { 111111 } password_confirmation {password} end end
user.spec.rb require 'rails_helper' RSpec.describe 'ユーザー新規登録', type: :system do before do @user = FactoryBot.build(:user) end context 'ユーザー新規登録ができるとき' do it '正しい情報を入力すればユーザー新規登録ができてトップページに移動する' do # トップページに移動する visit root_path # 新規登録ページへ移動する visit new_user_registration_path # ユーザー情報を入力する fill_in 'user_nickname', with: @user.nickname fill_in 'user_email', with: @user.email fill_in 'user_password', with: @user.password # サインアップページへ遷移するボタンやログインページへ遷移するボタンが表示されていないことを確認する expect(page).to have_no_content('新規登録') expect(page).to have_no_content('ログイン') end end context 'ユーザー新規登録ができないとき' do it '誤った情報ではユーザー新規登録ができずに新規登録ページへ戻ってくる' do # トップページに移動する visit root_path # 新規登録ページへ移動する visit new_user_registration_path # ユーザー情報を入力する fill_in 'user_nickname', with: "" fill_in 'user_email', with: "" fill_in 'user_password', with: "" fill_in 'user_password', with: "" fill_in 'user_password', with: "" # サインアップボタンを押してもユーザーモデルのカウントは上がらないことを確認する expect{ find('input[name="commit"]').click }.to change { User.count }.by(0) # 新規登録ページへ戻されることを確認する expect(current_path).to eq "/users" end end end RSpec.describe 'ログイン', type: :system do before do @user = FactoryBot.create(:user) end context 'ログインができるとき' do it '保存されているユーザーの情報と合致すればログインができる' do # トップページに移動する visit root_path # ログインページへ遷移する visit new_user_session_path # 正しいユーザー情報を入力する fill_in 'user_email', with: @user.email fill_in 'user_password', with: @user.password # ログインボタンを押す find('input[name="commit"]').click # トップページへ遷移することを確認する expect(current_path).to eq root_path end end context 'ログインができないとき' do it '保存されているユーザーの情報と合致しないとログインができない' do # トップページに移動する visit root_path # ログインページへ遷移する visit new_user_session_path # ユーザー情報を入力する fill_in 'user_email', with: "" fill_in 'user_password', with: "" # ログインボタンを押す find('input[name="commit"]').click # ログインページへ戻されることを確認する expect(current_path).to eq new_user_session_path end end end

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

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

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

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

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

hatsu

2020/08/30 00:36

spec/factories/books.rbで適切にUserを紐付けている必要があると思われます。 spec/factories/books.rbとspec/factories/users.rbの中身を記載いただくと回答が得られるかもです。
tomsuma

2020/08/30 00:40

ご回答ありがとうございます! ただ今載せさせてもらいました!
guest

回答1

0

ベストアンサー

  • User has many books
  • book belongs to User

の想定で回答します。

以下のようだといかがでしょうか
app/models/user.rb

class User < ApplicationRecord has_many :books end

app/models/book.rb

class Book < ApplicationRecord belongs_to :user end

spec/factories/user.rb

FactoryBot.define do factory :user do nickname { 'suzuki' } email { 'example@cmail.com' } password { 111111 } password_confirmation {password} end end

spec/factories/book.rb

FactoryBot.define do factory :book do association :user #ここでテストデータでユーザーの紐付けを行います name { '人間失格' } content { '紹介文' } genre_id { '5' } end end

投稿2020/08/30 00:49

hatsu

総合スコア1809

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

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

tomsuma

2020/08/30 04:41

できました! ありがとうございます!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問