Skip to content

Instantly share code, notes, and snippets.

@chesterbr
Created October 3, 2013 15:59
Show Gist options
  • Save chesterbr/6812236 to your computer and use it in GitHub Desktop.
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)
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