Skip to content

Instantly share code, notes, and snippets.

@soulcutter
Last active December 20, 2015 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soulcutter/6092223 to your computer and use it in GitHub Desktop.
Save soulcutter/6092223 to your computer and use it in GitHub Desktop.
instance_method owners don't always respond to method_defined?
module Barator
def foo(*args)
"#{super(*args)}bar"
end
end
class Foo
prepend Barator
def foo
'foo'
end
def to_s
foo
end
end
describe Foo do
specify { expect(Foo.new.foo).to eq 'foobar' }
[:foo, :to_s].each do |method_name|
context "##{method_name}" do
it "has an owner that responds to method_defined?" do
expect(Foo.instance_method(method_name).owner).to respond_to(:method_defined?)
end
it "is stubbable" do
expect { Foo.new.stub(method_name) }.to_not raise_error
end
end
end
end
Foo ...FF
1) Foo#to_s has an owner that responds to method_defined?
Failure/Error: expect(Foo.instance_method(method_name).owner).to respond_to(:method_defined?)
expected #<Foo:0x007f960d393118> to respond to :method_defined?
# ./spec/models/barator_spec.rb:24:in `block (4 levels) in <top (required)>'
2) Foo#to_s is stubbable
Failure/Error: expect { Foo.new.stub(method_name) }.to_not raise_error
expected no Exception, got #<NoMethodError: undefined method `method_defined?' for #<Foo:0x007f960d393118>> with backtrace:
# ./spec/models/barator_spec.rb:28:in `block (5 levels) in <top (required)>'
# ./spec/models/barator_spec.rb:28:in `block (4 levels) in <top (required)>'
# ./spec/models/barator_spec.rb:28:in `block (4 levels) in <top (required)>'
Finished in 0.00682 seconds
5 examples, 2 failures
Failed examples:
rspec ./spec/models/barator_spec.rb:23 # Foo#to_s has an owner that responds to method_defined?
rspec ./spec/models/barator_spec.rb:27 # Foo#to_s is stubbable
Randomized with seed 573
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment