Created
October 3, 2013 15:59
-
-
Save chesterbr/6812236 to your computer and use it in GitHub Desktop.
Get the described instance method name(as long as it follows the `describe '#method_name'` convention) in RSpec from within a shared spec, so it can call the method regardless of what it is. Useful when different methods share the same behavior (as an alternative to a block or paramter with the method name)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe SomeClass do | |
shared_examples_for 'a nice method' do | |
let(:method) { example.metadata[:example_group][:example_group][:description].gsub /\#/,'' } | |
it 'should do nice things' do | |
... | |
# This will call subject.foo_method or subject.bar_method, according to context | |
subject.send(method) | |
... | |
end | |
end | |
describe '#foo_method' do | |
it_behaves_like 'a nice method' | |
... | |
end | |
describe '#bar_method' do | |
it_behaves_like 'a nice method' | |
... | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment