Skip to content

Instantly share code, notes, and snippets.

@dchelimsky
Forked from cjbottaro/gist:1128091
Created August 6, 2011 15:12
Show Gist options
  • Save dchelimsky/1129417 to your computer and use it in GitHub Desktop.
Save dchelimsky/1129417 to your computer and use it in GitHub Desktop.
using shared_context with a block
# The following is an example of how you might use shared content to reduce the
# duplication in https://gist.github.com/1128108. I prefer to avoid examples
# that use `should respond_to`, as it specifies structure rather than behavior.
# I'd recommend specifying something about what happens when the message is
# sent, e.g. subject.some_method.should eq(some_value). That implicitly
# specifies that the object responds to some_method, and provides more useful
# direction/information about how the object should behave.
#
# That said ...
RSpec.configure do |c|
c.alias_it_should_behave_like_to :can_be_extended_with, "extended with"
# ^^ use can_be_extended_with in a group and the output will say "extended
# with"
end
shared_examples MyModule do
before(:each){ subject.extend(MyModule) }
it { should respond_to(:some_method) }
end
describe Object do
extended_with MyModule
end
describe Class do
extended_with MyModule do
it { should respond_to(:special_method_for_class) }
end
end
Object
extended with MyModule
should respond to #some_method
Class
extended with MyModule
should respond to #some_method
should respond to #special_method_for_class
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment