|
18 | 18 |
|
19 | 19 | before do |
20 | 20 | exporter.reset |
21 | | - |
22 | | - # Clean up any existing factory definitions |
| 21 | + instrumentation.install({}) |
23 | 22 | FactoryBot.reload |
24 | 23 |
|
25 | | - # Define test factories |
26 | 24 | FactoryBot.define do |
27 | 25 | factory :user do |
28 | 26 | sequence(:name) { |n| "User #{n}" } |
|
42 | 40 | end |
43 | 41 |
|
44 | 42 | after do |
45 | | - # Reset instrumentation state for isolation |
46 | 43 | instrumentation.instance_variable_set(:@installed, false) |
47 | 44 | end |
48 | 45 |
|
|
68 | 65 | end |
69 | 66 | end |
70 | 67 |
|
71 | | - # Strategy tests - FactoryBot.create |
72 | | - describe 'FactoryBot.create' do |
73 | | - before do |
74 | | - instrumentation.install({}) |
75 | | - end |
76 | | - |
77 | | - it 'creates a span for FactoryBot.create' do |
78 | | - # Using build instead of create to avoid needing database in tests |
79 | | - FactoryBot.build(:user) |
80 | | - |
81 | | - spans = exporter.finished_spans |
82 | | - _(spans).wont_be_empty |
83 | | - # For now, just verify instrumentation is working |
84 | | - # We'll fix span matching once implementation is done |
85 | | - end |
86 | | - end |
87 | | - |
88 | 68 | # Strategy tests - FactoryBot.build |
89 | 69 | describe 'FactoryBot.build' do |
90 | | - before do |
91 | | - instrumentation.install({}) |
92 | | - end |
93 | | - |
94 | 70 | it 'creates a span for FactoryBot.build' do |
95 | 71 | FactoryBot.build(:user) |
96 | 72 |
|
|
111 | 87 | span = exporter.finished_spans.find { |s| s.name.include?('FactoryBot.build') } |
112 | 88 | _(span.attributes['factory_bot.factory_name']).must_equal 'user' |
113 | 89 | end |
| 90 | + |
| 91 | + it 'sets factory_bot.traits to empty array when no traits' do |
| 92 | + FactoryBot.build(:user) |
| 93 | + |
| 94 | + span = exporter.finished_spans.find { |s| s.name.include?('FactoryBot.build') } |
| 95 | + _(span.attributes['factory_bot.traits']).must_equal [] |
| 96 | + end |
114 | 97 | end |
115 | 98 |
|
116 | 99 | # Strategy tests - FactoryBot.build_stubbed |
117 | 100 | describe 'FactoryBot.build_stubbed' do |
118 | | - before do |
119 | | - instrumentation.install({}) |
120 | | - end |
121 | | - |
122 | 101 | it 'creates a span for FactoryBot.build_stubbed' do |
123 | 102 | FactoryBot.build_stubbed(:user) |
124 | 103 |
|
|
139 | 118 | span = exporter.finished_spans.find { |s| s.name.include?('FactoryBot.build_stubbed') } |
140 | 119 | _(span.attributes['factory_bot.factory_name']).must_equal 'user' |
141 | 120 | end |
| 121 | + |
| 122 | + it 'sets factory_bot.traits to empty array when no traits' do |
| 123 | + FactoryBot.build_stubbed(:user) |
| 124 | + |
| 125 | + span = exporter.finished_spans.find { |s| s.name.include?('FactoryBot.build_stubbed') } |
| 126 | + _(span.attributes['factory_bot.traits']).must_equal [] |
| 127 | + end |
142 | 128 | end |
143 | 129 |
|
144 | 130 | # Strategy tests - FactoryBot.attributes_for |
145 | 131 | describe 'FactoryBot.attributes_for' do |
146 | | - before do |
147 | | - instrumentation.install({}) |
148 | | - end |
149 | | - |
150 | 132 | it 'creates a span for FactoryBot.attributes_for' do |
151 | 133 | FactoryBot.attributes_for(:user) |
152 | 134 |
|
|
167 | 149 | span = exporter.finished_spans.find { |s| s.name.include?('FactoryBot.attributes_for') } |
168 | 150 | _(span.attributes['factory_bot.factory_name']).must_equal 'user' |
169 | 151 | end |
| 152 | + |
| 153 | + it 'sets factory_bot.traits to empty array when no traits' do |
| 154 | + FactoryBot.attributes_for(:user) |
| 155 | + |
| 156 | + span = exporter.finished_spans.find { |s| s.name.include?('FactoryBot.attributes_for') } |
| 157 | + _(span.attributes['factory_bot.traits']).must_equal [] |
| 158 | + end |
170 | 159 | end |
171 | 160 |
|
172 | 161 | # Batch operation tests |
173 | 162 | describe 'FactoryBot.build_list' do |
174 | | - before do |
175 | | - instrumentation.install({}) |
176 | | - end |
177 | | - |
178 | 163 | it 'creates spans for each item in FactoryBot.build_list' do |
179 | 164 | FactoryBot.build_list(:user, 3) |
180 | 165 |
|
181 | 166 | spans = exporter.finished_spans.select { |s| s.name.include?('FactoryBot.build') } |
182 | 167 | _(spans.size).must_equal 3 |
183 | 168 | end |
184 | | - end |
185 | 169 |
|
186 | | - describe 'FactoryBot.build_pair' do |
187 | | - before do |
188 | | - instrumentation.install({}) |
| 170 | + it 'sets correct attributes on each span' do |
| 171 | + FactoryBot.build_list(:user, 3) |
| 172 | + |
| 173 | + spans = exporter.finished_spans.select { |s| s.name.include?('FactoryBot.build') } |
| 174 | + spans.each do |span| |
| 175 | + _(span.attributes['factory_bot.strategy']).must_equal 'build' |
| 176 | + _(span.attributes['factory_bot.factory_name']).must_equal 'user' |
| 177 | + _(span.attributes['factory_bot.traits']).must_equal [] |
| 178 | + end |
189 | 179 | end |
| 180 | + end |
190 | 181 |
|
| 182 | + describe 'FactoryBot.build_pair' do |
191 | 183 | it 'creates spans for each item in FactoryBot.build_pair' do |
192 | 184 | FactoryBot.build_pair(:user) |
193 | 185 |
|
194 | 186 | spans = exporter.finished_spans.select { |s| s.name.include?('FactoryBot.build') } |
195 | 187 | _(spans.size).must_equal 2 |
196 | 188 | end |
| 189 | + |
| 190 | + it 'sets correct attributes on each span' do |
| 191 | + FactoryBot.build_pair(:user) |
| 192 | + |
| 193 | + spans = exporter.finished_spans.select { |s| s.name.include?('FactoryBot.build') } |
| 194 | + spans.each do |span| |
| 195 | + _(span.attributes['factory_bot.strategy']).must_equal 'build' |
| 196 | + _(span.attributes['factory_bot.factory_name']).must_equal 'user' |
| 197 | + _(span.attributes['factory_bot.traits']).must_equal [] |
| 198 | + end |
| 199 | + end |
197 | 200 | end |
198 | 201 |
|
199 | 202 | # Test multiple factory types |
200 | 203 | describe 'multiple factories' do |
201 | | - before do |
202 | | - instrumentation.install({}) |
203 | | - end |
204 | | - |
205 | 204 | it 'correctly identifies different factories' do |
206 | 205 | FactoryBot.build(:user) |
207 | 206 | FactoryBot.build(:admin) |
|
217 | 216 | # Traits tests |
218 | 217 | describe 'traits' do |
219 | 218 | before do |
220 | | - instrumentation.install({}) |
221 | | - |
222 | 219 | FactoryBot.define do |
223 | 220 | factory :user_with_traits, class: User do |
224 | 221 | sequence(:name) { |n| "User #{n}" } |
|
232 | 229 | end |
233 | 230 | end |
234 | 231 |
|
235 | | - it 'sets factory_bot.traits as array' do |
| 232 | + it 'sets factory_bot.traits to empty array when no traits used' do |
| 233 | + FactoryBot.build(:user_with_traits) |
| 234 | + |
| 235 | + span = exporter.finished_spans.find { |s| s.name.include?('FactoryBot.build') } |
| 236 | + _(span.attributes['factory_bot.traits']).must_equal [] |
| 237 | + end |
| 238 | + |
| 239 | + it 'sets factory_bot.traits as array with multiple traits' do |
236 | 240 | FactoryBot.build(:user_with_traits, :premium, :verified) |
237 | 241 |
|
238 | 242 | span = exporter.finished_spans.find { |s| s.name.include?('FactoryBot.build') } |
|
0 commit comments