|
| 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