Skip to content

Commit 0fbc9de

Browse files
committed
Add unit tests for FactoryBot::FactoryRunner
1 parent 26f3680 commit 0fbc9de

1 file changed

Lines changed: 101 additions & 0 deletions

File tree

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
describe FactoryBot::FactoryRunner do
2+
it "looks up and runs the factory" do
3+
factory = double("factory", compile: nil, run: "result")
4+
allow(factory).to receive(:with_traits).and_return(factory)
5+
allow(FactoryBot::Internal).to receive(:factory_by_name).with(:user).and_return(factory)
6+
runner = FactoryBot::FactoryRunner.new(:user, :build, [])
7+
8+
runner.run
9+
10+
expect(factory).to have_received(:compile)
11+
expect(factory).to have_received(:run).with(:build, {})
12+
end
13+
14+
it "applies traits when present" do
15+
factory = double("factory", compile: nil, run: "result")
16+
factory_with_traits = double("factory_with_traits", run: "result")
17+
allow(factory).to receive(:with_traits).with([:admin]).and_return(factory_with_traits)
18+
allow(FactoryBot::Internal).to receive(:factory_by_name).with(:user).and_return(factory)
19+
runner = FactoryBot::FactoryRunner.new(:user, :build, [:admin])
20+
21+
runner.run
22+
23+
expect(factory).to have_received(:with_traits).with([:admin])
24+
expect(factory_with_traits).to have_received(:run).with(:build, {})
25+
end
26+
27+
it "does not call with_traits when no traits are given" do
28+
factory = double("factory", compile: nil, run: "result")
29+
allow(factory).to receive(:with_traits)
30+
allow(FactoryBot::Internal).to receive(:factory_by_name).with(:user).and_return(factory)
31+
runner = FactoryBot::FactoryRunner.new(:user, :build, [])
32+
33+
runner.run
34+
35+
expect(factory).not_to have_received(:with_traits)
36+
end
37+
38+
it "passes overrides to the factory" do
39+
factory = double("factory", compile: nil, run: "result")
40+
allow(FactoryBot::Internal).to receive(:factory_by_name).with(:user).and_return(factory)
41+
runner = FactoryBot::FactoryRunner.new(:user, :build, [{name: "Test"}])
42+
43+
runner.run
44+
45+
expect(factory).to have_received(:run).with(:build, {name: "Test"})
46+
end
47+
48+
it "fires before_run_factory instrumentation event" do
49+
factory = double("factory", compile: nil, run: "result")
50+
allow(FactoryBot::Internal).to receive(:factory_by_name).with(:user).and_return(factory)
51+
events = []
52+
subscription = ActiveSupport::Notifications.subscribe("factory_bot.before_run_factory") do |*args|
53+
events << args
54+
end
55+
runner = FactoryBot::FactoryRunner.new(:user, :build, [])
56+
57+
runner.run
58+
59+
expect(events.length).to eq 1
60+
ensure
61+
ActiveSupport::Notifications.unsubscribe(subscription)
62+
end
63+
64+
it "fires run_factory instrumentation event" do
65+
factory = double("factory", compile: nil, run: "result")
66+
allow(FactoryBot::Internal).to receive(:factory_by_name).with(:user).and_return(factory)
67+
events = []
68+
subscription = ActiveSupport::Notifications.subscribe("factory_bot.run_factory") do |*args|
69+
events << args
70+
end
71+
runner = FactoryBot::FactoryRunner.new(:user, :build, [])
72+
73+
runner.run
74+
75+
expect(events.length).to eq 1
76+
ensure
77+
ActiveSupport::Notifications.unsubscribe(subscription)
78+
end
79+
80+
it "accepts a different runner strategy" do
81+
factory = double("factory", compile: nil, run: "result")
82+
allow(FactoryBot::Internal).to receive(:factory_by_name).with(:user).and_return(factory)
83+
runner = FactoryBot::FactoryRunner.new(:user, :build, [])
84+
85+
runner.run(:create)
86+
87+
expect(factory).to have_received(:run).with(:create, {})
88+
end
89+
90+
it "yields the block to the factory" do
91+
block_called = false
92+
factory = double("factory", compile: nil)
93+
allow(factory).to receive(:run) { |*, &block| block&.call }
94+
allow(FactoryBot::Internal).to receive(:factory_by_name).with(:user).and_return(factory)
95+
runner = FactoryBot::FactoryRunner.new(:user, :build, [])
96+
97+
runner.run { block_called = true }
98+
99+
expect(block_called).to be true
100+
end
101+
end

0 commit comments

Comments
 (0)