Skip to content

Commit

Permalink
fixup! Fix features
Browse files Browse the repository at this point in the history
  • Loading branch information
pirj committed Feb 1, 2021
1 parent f03764c commit 21a7372
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 17 deletions.
36 changes: 26 additions & 10 deletions features/working_with_legacy_code/any_instance.feature
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,12 @@ Feature: Any Instance
"""ruby
RSpec.describe "allow_any_instance_of" do
it "returns the specified value on any instance of the class" do
allow_any_instance_of(Object).to receive(:foo).and_return(:return_value)
class Klass
def foo; end
end
allow_any_instance_of(Klass).to receive(:foo).and_return(:return_value)
o = Object.new
o = Klass.new
expect(o.foo).to eq(:return_value)
end
end
Expand All @@ -47,9 +50,13 @@ Feature: Any Instance
RSpec.describe "allow_any_instance_of" do
context "with receive_messages" do
it "stubs multiple methods" do
allow_any_instance_of(Object).to receive_messages(:foo => 'foo', :bar => 'bar')
class Klass
def foo; end
def bar; end
end
allow_any_instance_of(Klass).to receive_messages(:foo => 'foo', :bar => 'bar')
o = Object.new
o = Klass.new
expect(o.foo).to eq('foo')
expect(o.bar).to eq('bar')
end
Expand All @@ -65,10 +72,13 @@ Feature: Any Instance
RSpec.describe "allow_any_instance_of" do
context "with arguments" do
it "returns the stubbed value when arguments match" do
allow_any_instance_of(Object).to receive(:foo).with(:param_one, :param_two).and_return(:result_one)
allow_any_instance_of(Object).to receive(:foo).with(:param_three, :param_four).and_return(:result_two)
class Klass
def foo(one, two); end
end
allow_any_instance_of(Klass).to receive(:foo).with(:param_one, :param_two).and_return(:result_one)
allow_any_instance_of(Klass).to receive(:foo).with(:param_three, :param_four).and_return(:result_two)
o = Object.new
o = Klass.new
expect(o.foo(:param_one, :param_two)).to eq(:result_one)
expect(o.foo(:param_three, :param_four)).to eq(:result_two)
end
Expand Down Expand Up @@ -99,15 +109,21 @@ Feature: Any Instance
"""ruby
RSpec.describe "expect_any_instance_of" do
before do
expect_any_instance_of(Object).to receive(:foo)
expect_any_instance_of(klass).to receive(:foo)
end
let(:klass) do
Class.new do
def foo; end
end
end
it "passes when an instance receives the message" do
Object.new.foo
klass.new.foo
end
it "fails when no instance receives the message" do
Object.new.to_s
klass.new.to_s
end
end
"""
Expand Down
20 changes: 13 additions & 7 deletions features/working_with_legacy_code/message_chains.feature
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Feature: Message Chains
allow(double).to receive_message_chain("foo.bar") { :baz }
allow(double).to receive_message_chain(:foo, :bar => :baz)
allow(double).to receive_message_chain(:foo, :bar) { :baz }
````
```

Given any of these three forms:

Expand Down Expand Up @@ -74,19 +74,25 @@ Feature: Message Chains
Given a file named "receive_message_chain_spec.rb" with:
"""ruby
RSpec.describe "Using receive_message_chain on any instance of a class" do
let(:klass) do
Class.new do
def foo; end
end
end
example "using a string and a block" do
allow_any_instance_of(Object).to receive_message_chain("foo.bar") { :baz }
expect(Object.new.foo.bar).to eq(:baz)
allow_any_instance_of(klass).to receive_message_chain("foo.bar") { :baz }
expect(klass.new.foo.bar).to eq(:baz)
end
example "using symbols and a hash" do
allow_any_instance_of(Object).to receive_message_chain(:foo, :bar => :baz)
expect(Object.new.foo.bar).to eq(:baz)
allow_any_instance_of(klass).to receive_message_chain(:foo, :bar => :baz)
expect(klass.new.foo.bar).to eq(:baz)
end
example "using symbols and a block" do
allow_any_instance_of(Object).to receive_message_chain(:foo, :bar) { :baz }
expect(Object.new.foo.bar).to eq(:baz)
allow_any_instance_of(klass).to receive_message_chain(:foo, :bar) { :baz }
expect(klass.new.foo.bar).to eq(:baz)
end
end
"""
Expand Down

0 comments on commit 21a7372

Please sign in to comment.