Reputation: 347
I try to refactoring my specs.I have view spec/views/posts.html.haml_spec.rb
require 'rails_helper'
describe 'posts/show' do
before(:each) do
@post = assign(:post, create(:post))
@comments = assign(:comment, Kaminari.paginate_array([
create(:comment, post: @post)
]).page(1))
end
it 'renders attributes in <p>' do
render
end
end
and I want transfer of the code to factory spec/factories/posts.rb
FactoryGirl.define do
factory :post do
title Faker::Lorem.word
body Faker::Lorem.paragraph
trait :with_comments do
after(:create) do
create_list(:comment, post: post)
end
end
end
end
but when I running FactoryGirl.create(:post, :with_comments)
, shell show me an error
NameError: undefined local variable or method `post' for #<FactoryGirl::SyntaxRunner:0x00000003b44f08>
how fix?
sorry for my bad English
Upvotes: 1
Views: 1231
Reputation: 44685
You didn't pass object to after_create
block. You also didn;t specify the number of comments to be created. Change your trait to:
trait :with_comments do
after(:create) do |post|
create_list(:comment, <number_of_comments>, post: post)
end
end
Upvotes: 1