I am using Ruby on Rails 3.1.0 and the rspec-rails 2 gem. I would like to refactor the following sample code in a my spec file:
describe "Making things" do
it "should make a thing" do
# Make the thing
...
# This is the same statement as that present in the "should make another
# thing" example (read below for more information)
response.body.should include("Hello World")
end
it "should make another thing" do
# Make another thing
...
# The same statement as that present in the "should make a thing" example
response.body.should include("Hello World")
end
end
How can I refactor the above response.body.should include("Hello World") code so to write less code? That is, how can I test the response.body content by using one statement valid for both spec examples?
Use
shared_examples_for.Like this:
See documentation here.