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?
On Tue, Apr 22, 2008 at 9:36 AM, Perryn F. [email protected]
wrote:
response.should have_tag("div") do
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?
Another attack.
Then(“two nested divs and an? $type will be displayed”) do | type |
response.should have_tag(“div”) do
with_tag(“div”)
end
response.should have_tag(type == ‘image’ ? ‘img’ : type)
end
–
Rick DeNatale
My blog on Ruby
http://talklikeaduck.denhaven2.com/
Another attack.
Then(“two nested divs and an? $type will be displayed”) do | type |
response.should have_tag("div") do
with_tag("div")
end
response.should have_tag(type == 'image' ? 'img' : type)
end
Thanks for the cool idea Rick, but unfortunately that only helps with
my contrived little example -
for my real app I really do need to be able to factor out some helper
methods…
You should be able to make your own module of helpers and include it
into
Spec::Story::World
module Spec::Story::World
def your_helper_method
# i should have access to everything I need
end
end
Zach
Hi Perryn,
maybe you’ll find this interesting:
http://www.workunitgroup.com/2008/4/23/crafting-rspec-steps-with-
step_eval-and-drying-them-with-a-helper
The DRYing thing begins at the middle of the second part of this
series.
matthias
You should be able to make your own module of helpers and include it into
Spec::Story::World
module Spec::Story::World
def your_helper_method
# i should have access to everything I need
end
end
brilliant, worked like a charm thanks 
If you will indulge a ruby newb question for a moment - how could I
have worked this out for myself?