Skip to content

Commit 54c4dd3

Browse files
committed
Add unit tests for FactoryBot::Linter
1 parent 26f3680 commit 54c4dd3

1 file changed

Lines changed: 76 additions & 0 deletions

File tree

spec/factory_bot/linter_spec.rb

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
describe FactoryBot::Linter do
2+
it "does not raise when all factories are valid" do
3+
define_class("User") { attr_accessor :name }
4+
FactoryBot.define do
5+
factory :linter_user, class: "User" do
6+
name { "Test" }
7+
end
8+
end
9+
factories = FactoryBot.factories.select { |f| f.name == :linter_user }
10+
linter = FactoryBot::Linter.new(factories, strategy: :build)
11+
12+
expect { linter.lint! }.not_to raise_error
13+
end
14+
15+
it "raises InvalidFactoryError when a factory is invalid" do
16+
define_class("LinterFailModel") {
17+
def save!
18+
raise "validation failed"
19+
end
20+
}
21+
FactoryBot.define do
22+
factory :linter_fail_model, class: "LinterFailModel"
23+
end
24+
factories = FactoryBot.factories.select { |f| f.name == :linter_fail_model }
25+
linter = FactoryBot::Linter.new(factories, strategy: :create)
26+
27+
expect { linter.lint! }.to raise_error(FactoryBot::InvalidFactoryError)
28+
end
29+
30+
it "includes factory name in error message" do
31+
define_class("LinterBadModel") {
32+
def save!
33+
raise "cannot save"
34+
end
35+
}
36+
FactoryBot.define do
37+
factory :linter_bad_model, class: "LinterBadModel"
38+
end
39+
factories = FactoryBot.factories.select { |f| f.name == :linter_bad_model }
40+
linter = FactoryBot::Linter.new(factories, strategy: :create)
41+
42+
expect { linter.lint! }.to raise_error(/linter_bad_model/)
43+
end
44+
end
45+
46+
describe FactoryBot::Linter::FactoryError do
47+
it "formats error message with factory name and error details" do
48+
wrapped_error = RuntimeError.new("something went wrong")
49+
factory = double("factory", name: :user)
50+
error = FactoryBot::Linter::FactoryError.new(wrapped_error, factory)
51+
52+
expect(error.message).to include("user")
53+
expect(error.message).to include("something went wrong")
54+
expect(error.message).to include("RuntimeError")
55+
end
56+
57+
it "includes backtrace in verbose message" do
58+
wrapped_error = RuntimeError.new("something went wrong")
59+
wrapped_error.set_backtrace(["line1.rb:1", "line2.rb:2"])
60+
factory = double("factory", name: :user)
61+
error = FactoryBot::Linter::FactoryError.new(wrapped_error, factory)
62+
63+
expect(error.verbose_message).to include("line1.rb:1")
64+
expect(error.verbose_message).to include("line2.rb:2")
65+
end
66+
end
67+
68+
describe FactoryBot::Linter::FactoryTraitError do
69+
it "includes factory and trait name in location" do
70+
wrapped_error = RuntimeError.new("trait error")
71+
factory = double("factory", name: :user)
72+
error = FactoryBot::Linter::FactoryTraitError.new(wrapped_error, factory, :admin)
73+
74+
expect(error.message).to include("user+admin")
75+
end
76+
end

0 commit comments

Comments
 (0)