|
71 | 71 | FactoryBot.define do |
72 | 72 | before(:all) { TestLog << "global before-all called" } |
73 | 73 | after(:all) { TestLog << "global after-all called" } |
| 74 | + before(:build) { TestLog << "global before-build called" } |
74 | 75 | after(:build) { TestLog << "global after-build called" } |
75 | 76 | before(:create) { TestLog << "global before-create called" } |
76 | 77 | after(:create) { TestLog << "global after-create called" } |
77 | 78 |
|
78 | 79 | factory :parent, class: :user do |
79 | 80 | before(:all) { TestLog << "parent before-all called" } |
80 | 81 | after(:all) { TestLog << "parent after-all called" } |
| 82 | + before(:build) { TestLog << "parent before-build called" } |
81 | 83 | after(:build) { TestLog << "parent after-build called" } |
82 | 84 | before(:create) { TestLog << "parent before-create called" } |
83 | 85 | after(:create) { TestLog << "parent after-create called" } |
84 | 86 |
|
85 | 87 | trait :parent_trait_1 do |
86 | 88 | before(:all) { TestLog << "parent-trait-1 before-all called" } |
87 | 89 | after(:all) { TestLog << "parent-trait-1 after-all called" } |
| 90 | + before(:build) { TestLog << "parent-trait-1 before-build called" } |
88 | 91 | after(:build) { TestLog << "parent-trait-1 after-build called" } |
89 | 92 | before(:create) { TestLog << "parent-trait-1 before-create called" } |
90 | 93 | after(:create) { TestLog << "parent-trait-1 after-create called" } |
|
93 | 96 | trait :parent_trait_2 do |
94 | 97 | before(:all) { TestLog << "parent-trait-2 before-all called" } |
95 | 98 | after(:all) { TestLog << "parent-trait-2 after-all called" } |
| 99 | + before(:build) { TestLog << "parent-trait-2 before-build called" } |
96 | 100 | after(:build) { TestLog << "parent-trait-2 after-build called" } |
97 | 101 | before(:create) { TestLog << "parent-trait-2 before-create called" } |
98 | 102 | after(:create) { TestLog << "parent-trait-2 after-create called" } |
|
103 | 107 | before(:all) { TestLog << "child before-all called" } |
104 | 108 | after(:create) { TestLog << "child after-create called" } |
105 | 109 | after(:build) { TestLog << "child after-build called" } |
| 110 | + before(:build) { TestLog << "child before-build called" } |
106 | 111 | before(:create) { TestLog << "child before-create called" } |
107 | 112 | after(:all) { TestLog << "child after-all called" } |
108 | 113 |
|
109 | 114 | trait :child_trait do |
110 | 115 | before(:all) { TestLog << "child-trait before-all called" } |
111 | 116 | after(:all) { TestLog << "child-trait after-all called" } |
| 117 | + before(:build) { TestLog << "child-trait before-build called" } |
112 | 118 | after(:build) { TestLog << "child-trait after-build called" } |
113 | 119 | before(:create) { TestLog << "child-trait before-create called" } |
114 | 120 | after(:create) { TestLog << "child-trait after-create called" } |
|
125 | 131 | # |
126 | 132 | FactoryBot.create(:child, :parent_trait_2, :child_trait, :parent_trait_1) |
127 | 133 |
|
128 | | - expect(TestLog.size).to eq 30 |
| 134 | + expect(TestLog.size).to eq 36 |
129 | 135 |
|
130 | 136 | # before(:all) |
131 | 137 | expect(TestLog[0..5]).to eq [ |
|
137 | 143 | "parent-trait-1 before-all called" |
138 | 144 | ] |
139 | 145 |
|
140 | | - # after(:build) |
| 146 | + # before(:build) |
141 | 147 | expect(TestLog[6..11]).to eq [ |
| 148 | + "global before-build called", |
| 149 | + "parent before-build called", |
| 150 | + "child before-build called", |
| 151 | + "parent-trait-2 before-build called", |
| 152 | + "child-trait before-build called", |
| 153 | + "parent-trait-1 before-build called" |
| 154 | + ] |
| 155 | + |
| 156 | + # after(:build) |
| 157 | + expect(TestLog[12..17]).to eq [ |
142 | 158 | "global after-build called", |
143 | 159 | "parent after-build called", |
144 | 160 | "child after-build called", |
|
148 | 164 | ] |
149 | 165 |
|
150 | 166 | # before(:create) |
151 | | - expect(TestLog[12..17]).to eq [ |
| 167 | + expect(TestLog[18..23]).to eq [ |
152 | 168 | "global before-create called", |
153 | 169 | "parent before-create called", |
154 | 170 | "child before-create called", |
|
158 | 174 | ] |
159 | 175 |
|
160 | 176 | # after(:create) |
161 | | - expect(TestLog[18..23]).to eq [ |
| 177 | + expect(TestLog[24..29]).to eq [ |
162 | 178 | "global after-create called", |
163 | 179 | "parent after-create called", |
164 | 180 | "child after-create called", |
|
168 | 184 | ] |
169 | 185 |
|
170 | 186 | # after(:all) |
171 | | - expect(TestLog[24..29]).to eq [ |
| 187 | + expect(TestLog[30..35]).to eq [ |
172 | 188 | "global after-all called", |
173 | 189 | "parent after-all called", |
174 | 190 | "child after-all called", |
@@ -463,3 +479,33 @@ def name |
463 | 479 | expect(build(:company).name).to eq "ACME SUPPLIERS" |
464 | 480 | end |
465 | 481 | end |
| 482 | + |
| 483 | +describe "before build callback" do |
| 484 | + before do |
| 485 | + define_class("TitleSetter") do |
| 486 | + def self.title=(new_title) |
| 487 | + class_variable_set(:@@title, new_title) |
| 488 | + end |
| 489 | + |
| 490 | + def self.title |
| 491 | + class_variable_get(:@@title) |
| 492 | + end |
| 493 | + end |
| 494 | + |
| 495 | + define_model("Article", title: :string) |
| 496 | + |
| 497 | + FactoryBot.define do |
| 498 | + factory :article_with_before_callbacks, class: :article do |
| 499 | + before(:build) { TitleSetter.title = "title from before build" } |
| 500 | + after(:build) { TitleSetter.title = "title from after build" } |
| 501 | + |
| 502 | + title { TitleSetter.title } |
| 503 | + end |
| 504 | + end |
| 505 | + end |
| 506 | + |
| 507 | + it "runs the before callback" do |
| 508 | + article = FactoryBot.build(:article_with_before_callbacks) |
| 509 | + expect(article.title).to eq("title from before build") |
| 510 | + end |
| 511 | +end |
0 commit comments