[rspec-users] how do i remove duplication from my steps in story runner?

Perryn Fowler pezlists at gmail.com
Tue Apr 22 09:36:58 EDT 2008


OK, I'm probably being incredibly dense here

I have some steps defined something like this (extremely contrived) example


steps_for(:foo) do

 #snip Given and When steps

  Then("two nested divs and a image will be displayed") do
      response.should have_tag("div") do
            with_tag("div")
      end
      response.should have_tag("img")
  end

  Then("two nested divs and a table will be displayed") do
      response.should have_tag("div") do
            with_tag("div")
      end
      response.should have_tag("table")
  end
end

with_steps_for :foo do
   run 'stories/foo_story', :type => RailsStory
end


Now, I'd like to remove the duplication in there by extracting out a
'two_nested_divs_should_be_displayed' method so that my steps can read
more like



steps_for(:foo) do

 #snip Given and When steps

  Then("two nested divs and a image will be displayed") do
     two_nested_divs_should_be_displayed
      response.should have_tag("img")
  end

  Then("two nested divs and a table will be displayed") do
      two_nested_divs_should_be_displayed
      response.should have_tag("table")
  end
end

with_steps_for :foo do
   run 'stories/foo_story', :type => RailsStory
end

but the ruby magic going on is proving hard for me to untangle and I
can't for the life of me get it to work. I either get method_missing
looking for my method, or the method complains that 'with_tag' is
undefined.

any ideas?


More information about the rspec-users mailing list