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

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

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

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

RSpec

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

Ruby on Rails

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

Q&A

解決済

1回答

5301閲覧

子レコードが作成されたかどうかのテストが上手くいかない。RSpec

takeo7

総合スコア18

Ruby

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

RSpec

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

Ruby on Rails

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

0グッド

0クリップ

投稿2016/09/09 08:28

###前提・実現したいこと
RSpecのコントローラーのテストで、レコードが作成されているかテストをしています。
「親レコードが作成されているか」のテストはうまくいくのですが、「子レコードの作成ができているか」のテストがうまくいきません。
ちなみに、テスト対象の動きは実際に親レコード、子レコード共に作成されています。
どなたか子レコード作成のテストに関することをご教授ください!

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

Failure/Error: expect { post :create, event: attributes_for(:event_create) }.to change(User, :count).by(1) expected #count to have changed by 1, but was changed by 0

###該当のソースコード
コントローラーのテストコードはこちら↓

Ruby

1#/spec/controller/events_controller_spec.rb 2 3#====親レコードはパスする===== 4describe "Modelに正常な値をセット" do 5 it "EventsTableにレコードが1つ作成されること" do 6 expect { 7 post :create, event: attributes_for(:event) 8 }.to change(Event, :count).by(1) 9 end 10#====子レコードは作成できてないとエラーが出る==== 11 it "UserTableにレコードが1つ作成されること" do 12 expect { 13 post :create, event: attributes_for(:event_create) 14 }.to change(User, :count).by(1) 15 end

ファクトリの定義はこちら↓

Ruby

1#/spec/factories/events.rb 2FactoryGirl.define do 3 factory :event_create, class: Event do 4 title "title" 5 author_attributes { attributes_for(:user) } 6 end 7end 8 9#/spec/factories/users.rb 10FactoryGirl.define do 11 factory :user, class: User do 12 name "name" 13 end 14end

テスト対象のコントローラー↓

Ruby

1#/controllers/events_controller #create 2 def create 3 @event = Event.new(event_params) 4 if @event.save 5 redirect_to event_path(@event.id) 6 else 7 render new_event_path 8 end 9 end

テスト対象のモデル↓

Ruby

1#/models/event.rb 2class Event < ActiveRecord::Base 3 has_many :users 4 validates :title, presence: true 5 6#/models/user.rb 7class User < ActiveRecord::Base 8 belongs_to :event 9 validates :name, presence: true 10end

###補足情報(言語/FW/ツール等のバージョンなど)
RSpec -v "3.5.2"
FactoryGirl

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

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

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

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

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

guest

回答1

0

ベストアンサー

ruby

1#/spec/factories/events.rb 2FactoryGirl.define do 3 factory :event_create, class: Event do 4 title "title" 5 author_attributes { attributes_for(:user) } 6 end 7end

ruby

1author_attributes { attributes_for(:user) }

は、author_attributesではなくuser_attributes

ruby

1user_attributes { attributes_for(:user) }

になり、

ruby

1#/spec/factories/events.rb 2FactoryGirl.define do 3 factory :event_create, class: Event do 4 title "title" 5 user_attributes { attributes_for(:user) } 6 end 7end

が正しいのではないでしょうか?

投稿2016/10/12 14:04

cameluby

総合スコア891

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.49%

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

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

質問する

関連した質問