Skip to content

Commit 52d73fa

Browse files
committed
Add unit tests for Decorator, InvocationTracker, and NewConstructor
1 parent 26f3680 commit 52d73fa

3 files changed

Lines changed: 121 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
describe FactoryBot::Decorator::InvocationTracker do
2+
it "tracks method names that are called" do
3+
component = double("component", foo: nil, bar: nil)
4+
tracker = FactoryBot::Decorator::InvocationTracker.new(component)
5+
6+
tracker.foo
7+
tracker.bar
8+
9+
expect(tracker.__invoked_methods__).to eq [:foo, :bar]
10+
end
11+
12+
it "forwards method calls to the component" do
13+
component = double("component", foo: "result")
14+
tracker = FactoryBot::Decorator::InvocationTracker.new(component)
15+
16+
expect(tracker.foo).to eq "result"
17+
end
18+
19+
it "forwards arguments to the component" do
20+
component = double("component")
21+
allow(component).to receive(:foo).with(1, "two").and_return("result")
22+
tracker = FactoryBot::Decorator::InvocationTracker.new(component)
23+
24+
expect(tracker.foo(1, "two")).to eq "result"
25+
end
26+
27+
it "returns unique method names" do
28+
component = double("component", foo: nil)
29+
tracker = FactoryBot::Decorator::InvocationTracker.new(component)
30+
31+
tracker.foo
32+
tracker.foo
33+
34+
expect(tracker.__invoked_methods__).to eq [:foo]
35+
end
36+
37+
it "returns an empty array when no methods have been called" do
38+
component = double("component")
39+
tracker = FactoryBot::Decorator::InvocationTracker.new(component)
40+
41+
expect(tracker.__invoked_methods__).to eq []
42+
end
43+
end
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
describe FactoryBot::Decorator::NewConstructor do
2+
it "delegates new to the build class" do
3+
component = double("component")
4+
build_class = double("build_class")
5+
instance = double("instance")
6+
allow(build_class).to receive(:new).and_return(instance)
7+
decorator = FactoryBot::Decorator::NewConstructor.new(component, build_class)
8+
9+
expect(decorator.new).to eq instance
10+
end
11+
12+
it "passes arguments to the build class" do
13+
component = double("component")
14+
build_class = double("build_class")
15+
instance = double("instance")
16+
allow(build_class).to receive(:new).with("arg1", "arg2").and_return(instance)
17+
decorator = FactoryBot::Decorator::NewConstructor.new(component, build_class)
18+
19+
expect(decorator.new("arg1", "arg2")).to eq instance
20+
end
21+
22+
it "forwards other methods to the component" do
23+
component = double("component", foo: "bar")
24+
build_class = double("build_class")
25+
decorator = FactoryBot::Decorator::NewConstructor.new(component, build_class)
26+
27+
expect(decorator.foo).to eq "bar"
28+
end
29+
end

spec/factory_bot/decorator_spec.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
describe FactoryBot::Decorator do
2+
it "forwards method calls to the component" do
3+
component = double("component", foo: "bar")
4+
decorator = FactoryBot::Decorator.new(component)
5+
6+
expect(decorator.foo).to eq "bar"
7+
end
8+
9+
it "forwards method calls with arguments" do
10+
component = double("component")
11+
allow(component).to receive(:foo).with(1, 2).and_return("result")
12+
decorator = FactoryBot::Decorator.new(component)
13+
14+
expect(decorator.foo(1, 2)).to eq "result"
15+
end
16+
17+
it "forwards method calls with blocks" do
18+
component = Object.new
19+
def component.foo(&block) = block.call
20+
decorator = FactoryBot::Decorator.new(component)
21+
22+
expect(decorator.foo { "yielded" }).to eq "yielded"
23+
end
24+
25+
it "delegates send to __send__" do
26+
component = double("component", foo: "bar")
27+
decorator = FactoryBot::Decorator.new(component)
28+
29+
expect(decorator.send(:foo)).to eq "bar"
30+
end
31+
32+
it "returns true from respond_to? when the component responds to the method" do
33+
component = double("component", foo: "bar")
34+
decorator = FactoryBot::Decorator.new(component)
35+
36+
expect(decorator.respond_to?(:foo)).to be true
37+
end
38+
39+
it "returns false from respond_to? when the component does not respond" do
40+
component = double("component")
41+
decorator = FactoryBot::Decorator.new(component)
42+
43+
expect(decorator.respond_to?(:nonexistent)).to be false
44+
end
45+
46+
it "resolves constants from Object" do
47+
expect(FactoryBot::Decorator::String).to eq ::String
48+
end
49+
end

0 commit comments

Comments
 (0)