/
create_list_spec.rb
97 lines (79 loc) · 2.25 KB
/
create_list_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
require 'spec_helper'
describe "create multiple instances" do
before do
define_model('Post', title: :string, position: :integer)
FactoryGirl.define do
factory(:post) do |post|
post.title "Through the Looking Glass"
post.position { rand(10**4) }
end
end
end
context "without default attributes" do
subject { FactoryGirl.create_list(:post, 20) }
its(:length) { should == 20 }
it "creates all the posts" do
subject.each do |record|
record.should_not be_new_record
end
end
it "uses the default factory values" do
subject.each do |record|
record.title.should == "Through the Looking Glass"
end
end
end
context "with default attributes" do
subject { FactoryGirl.create_list(:post, 20, title: "The Hunting of the Snark") }
it "overrides the default values" do
subject.each do |record|
record.title.should == "The Hunting of the Snark"
end
end
end
context "with a block" do
subject do
FactoryGirl.create_list(:post, 20, title: "The Listing of the Block") do |post|
post.position = post.id
end
end
it "uses the new values" do
subject.each_with_index do |record, index|
record.position.should == record.id
end
end
end
end
describe "multiple creates and ignored attributes to dynamically build attribute lists" do
before do
define_model('User', name: :string) do
has_many :posts
end
define_model('Post', title: :string, user_id: :integer) do
belongs_to :user
end
FactoryGirl.define do
factory :post do
title "Through the Looking Glass"
user
end
factory :user do
name "John Doe"
factory :user_with_posts do
ignore do
posts_count 5
end
after(:create) do |user, evaluator|
FactoryGirl.create_list(:post, evaluator.posts_count, user: user)
end
end
end
end
end
it "generates the correct number of posts" do
FactoryGirl.create(:user_with_posts).posts.length.should == 5
end
it "allows the number of posts to be modified" do
FactoryGirl.create(:user_with_posts, posts_count: 2).posts.length.should == 2
end
end