ruby - Rewrite shared example groups in rspec2 -
in rspec 1
describe "something", :shared => true include somemodule # has :a_method method def a_method(options) super(options.merge(:option => @attr) end "foofoofoo" end end describe "something else" before(:each) @attr = :s_else end it_should_behave_like "something" "barbarbar" a_method(:name => "something else") something.find("something else").name.should == "something else" end ... end
that is, use :shared => true
not refactor examples share method definitions , attributes. realize example contrived, how 1 write in rspec >= 2 without touching somemodule
module or something
class?
you can shared_examples_for
shared_examples_for "something" include somemodule # has :a_method method def a_method(options) super(options.merge(:option => @attr)) end "foofoofoo" end end
and call it_behaves_like
:
it_behaves_like "something"
edit
joao correctly points out fails include somemodule examples in describe block. include have take place outside shared example group, e.g. @ top of spec file
include somemodule # has :a_method method # ... shared_examples_for "something" def a_method(options) super(options.merge(:option => @attr)) end "foofoofoo" end end
david chelimsky discusses new features of shared examples in rspec 2 may pertinent in this blog post.
Comments
Post a Comment