-
-
Notifications
You must be signed in to change notification settings - Fork 359
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pass the instance to any instance stubs. #351
Changes from 3 commits
6287f7a
a37e021
4cb7c1a
e3d825a
95476b1
7be6505
7feb8a1
7293bfa
c104d49
a4f908f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ def initialize(error_generator, expectation_ordering, expected_from, method_doub | |
@args_to_yield = [] | ||
@failed_fast = nil | ||
@eval_context = nil | ||
@is_any_instance_expectation = opts[:is_any_instance_expectation] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This isn't used anymore, right? |
||
|
||
@implementation = Implementation.new | ||
self.inner_implementation_action = implementation_block | ||
|
@@ -168,6 +169,10 @@ def and_yield(*args, &block) | |
|
||
# @private | ||
def matches?(message, *args) | ||
args = args.dup | ||
if @is_any_instance_expectation && ::RSpec::Mocks.configuration.pass_instance_to_any_instance_stubs | ||
args.shift | ||
end | ||
@message == message && @argument_list_matcher.args_match?(*args) | ||
end | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -830,6 +830,71 @@ def foo; end | |
end | ||
end | ||
|
||
context "passing self" do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
context "when configured to pass the instance" do | ||
before(:each) do | ||
@orig_pass = RSpec::Mocks.configuration.pass_instance_to_any_instance_stubs | ||
RSpec::Mocks.configuration.pass_instance_to_any_instance_stubs = true | ||
end | ||
|
||
after(:each) do | ||
RSpec::Mocks.configuration.pass_instance_to_any_instance_stubs = @orig_pass | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rather than putting this here, what do you think about creating a |
||
|
||
describe "an any instance stub" do | ||
it "receives the instance" do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about |
||
klass = Struct.new(:science) | ||
instance = klass.new | ||
klass.any_instance.stub(:bees) { |*args| expect(args.first).to eq(instance) } | ||
instance.bees | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could benefit from my suggested alternative below... |
||
end | ||
end | ||
|
||
describe "an any instance expectation" do | ||
it "doesn't effect with" do | ||
klass = Struct.new(:science) | ||
instance = klass.new | ||
klass.any_instance.should_receive(:bees).with(:sup) | ||
instance.bees(:sup) | ||
end | ||
|
||
it "passes the instance" do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about |
||
klass = Struct.new(:science) | ||
instance = klass.new | ||
klass.any_instance.should_receive(:bees).with(:sup) { |*args| expect(args.first).to eq(instance) } | ||
instance.bees(:sup) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggested alternative: instance = klass.new
expect { |b|
klass.any_instance.should_receive(:bees).with(:sup, &b)
instance.bees(:sup)
}.to yield_with_args(instance, :sup) Reasons I like this better:
|
||
end | ||
end | ||
end | ||
|
||
context "when configured not to pass the instance" do | ||
before(:each) do | ||
@orig_pass = RSpec::Mocks.configuration.pass_instance_to_any_instance_stubs | ||
RSpec::Mocks.configuration.pass_instance_to_any_instance_stubs = false | ||
end | ||
|
||
after(:each) do | ||
RSpec::Mocks.configuration.pass_instance_to_any_instance_stubs = @orig_pass | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you extract the shared context I suggested above, it could be used here as well :). |
||
|
||
describe "an any instance stub" do | ||
it "does not receive the instance" do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How about |
||
klass = Struct.new(:science) | ||
instance = klass.new | ||
klass.any_instance.stub(:bees) { |*args| expect(args).to be_empty } | ||
instance.bees | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could benefit from my suggested alternative above... |
||
end | ||
|
||
it "gets data from with correctly" do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doc string doesn't make much sense to me...what are you trying to test here? |
||
klass = Struct.new(:science) | ||
instance = klass.new | ||
klass.any_instance.should_receive(:bees).with(:faces) | ||
instance.bees(:faces) | ||
end | ||
end | ||
end | ||
end | ||
|
||
context 'when used in conjunction with a `dup`' do | ||
it "doesn't cause an infinite loop" do | ||
pending "This intermittently fails on JRuby" if RUBY_PLATFORM == 'java' | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
yield_instance_from_any_instance_implementation_blocks
is a better name. What do you think?