Skip to content

Commit 30a3ea8

Browse files
committed
refactor: Improve test structure and coverage
- Move instrumentation.install to top-level before block - Remove redundant install calls from nested describe blocks - Add comprehensive attribute tests for all strategies - Test empty traits array in all relevant sections - Ensure all 3 attributes tested: strategy, factory_name, traits
1 parent 3853bbd commit 30a3ea8

File tree

1 file changed

+52
-48
lines changed

1 file changed

+52
-48
lines changed

instrumentation/factory_bot/test/opentelemetry/instrumentation/factory_bot/instrumentation_test.rb

Lines changed: 52 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@
1818

1919
before do
2020
exporter.reset
21-
22-
# Clean up any existing factory definitions
21+
instrumentation.install({})
2322
FactoryBot.reload
2423

25-
# Define test factories
2624
FactoryBot.define do
2725
factory :user do
2826
sequence(:name) { |n| "User #{n}" }
@@ -42,7 +40,6 @@
4240
end
4341

4442
after do
45-
# Reset instrumentation state for isolation
4643
instrumentation.instance_variable_set(:@installed, false)
4744
end
4845

@@ -68,29 +65,8 @@
6865
end
6966
end
7067

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-
8868
# Strategy tests - FactoryBot.build
8969
describe 'FactoryBot.build' do
90-
before do
91-
instrumentation.install({})
92-
end
93-
9470
it 'creates a span for FactoryBot.build' do
9571
FactoryBot.build(:user)
9672

@@ -111,14 +87,17 @@
11187
span = exporter.finished_spans.find { |s| s.name.include?('FactoryBot.build') }
11288
_(span.attributes['factory_bot.factory_name']).must_equal 'user'
11389
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
11497
end
11598

11699
# Strategy tests - FactoryBot.build_stubbed
117100
describe 'FactoryBot.build_stubbed' do
118-
before do
119-
instrumentation.install({})
120-
end
121-
122101
it 'creates a span for FactoryBot.build_stubbed' do
123102
FactoryBot.build_stubbed(:user)
124103

@@ -139,14 +118,17 @@
139118
span = exporter.finished_spans.find { |s| s.name.include?('FactoryBot.build_stubbed') }
140119
_(span.attributes['factory_bot.factory_name']).must_equal 'user'
141120
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
142128
end
143129

144130
# Strategy tests - FactoryBot.attributes_for
145131
describe 'FactoryBot.attributes_for' do
146-
before do
147-
instrumentation.install({})
148-
end
149-
150132
it 'creates a span for FactoryBot.attributes_for' do
151133
FactoryBot.attributes_for(:user)
152134

@@ -167,41 +149,58 @@
167149
span = exporter.finished_spans.find { |s| s.name.include?('FactoryBot.attributes_for') }
168150
_(span.attributes['factory_bot.factory_name']).must_equal 'user'
169151
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
170159
end
171160

172161
# Batch operation tests
173162
describe 'FactoryBot.build_list' do
174-
before do
175-
instrumentation.install({})
176-
end
177-
178163
it 'creates spans for each item in FactoryBot.build_list' do
179164
FactoryBot.build_list(:user, 3)
180165

181166
spans = exporter.finished_spans.select { |s| s.name.include?('FactoryBot.build') }
182167
_(spans.size).must_equal 3
183168
end
184-
end
185169

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
189179
end
180+
end
190181

182+
describe 'FactoryBot.build_pair' do
191183
it 'creates spans for each item in FactoryBot.build_pair' do
192184
FactoryBot.build_pair(:user)
193185

194186
spans = exporter.finished_spans.select { |s| s.name.include?('FactoryBot.build') }
195187
_(spans.size).must_equal 2
196188
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
197200
end
198201

199202
# Test multiple factory types
200203
describe 'multiple factories' do
201-
before do
202-
instrumentation.install({})
203-
end
204-
205204
it 'correctly identifies different factories' do
206205
FactoryBot.build(:user)
207206
FactoryBot.build(:admin)
@@ -217,8 +216,6 @@
217216
# Traits tests
218217
describe 'traits' do
219218
before do
220-
instrumentation.install({})
221-
222219
FactoryBot.define do
223220
factory :user_with_traits, class: User do
224221
sequence(:name) { |n| "User #{n}" }
@@ -232,7 +229,14 @@
232229
end
233230
end
234231

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
236240
FactoryBot.build(:user_with_traits, :premium, :verified)
237241

238242
span = exporter.finished_spans.find { |s| s.name.include?('FactoryBot.build') }

0 commit comments

Comments
 (0)