[rspec-users] RSpec and Watir, easy script structure question

David Chelimsky dchelimsky at gmail.com
Wed Mar 3 08:15:09 EST 2010


On Tue, Mar 2, 2010 at 7:40 PM, Adam Reed <reed.adam at gmail.com> wrote:
> On Tue, Mar 2, 2010 at 6:11 PM, David Chelimsky <dchelimsky at gmail.com>
> wrote:
>>
>> On Tue, Mar 2, 2010 at 11:24 AM, Adam R <reed.adam at gmail.com> wrote:
>> >
>> > Howdy!  I'm relatively new to RSpec, but I've worked with Watir and Ruby
>> > for
>> > about 5 years now.  My company is redesigning a web app from the ground
>> > up,
>> > so I'm taking the opportunity to leverage RSpec instead of Test/Unit.
>> >
>> > I've run into a problem with RSpec organization that I just can't sort
>> > out
>> > on my own, so I'm hoping it's straightforward enough that someone can
>> > help
>> > me out.
>> >
>> > Basically I have a single HTML page with an organized list of links.
>> >  Each
>> > link points to a new page with a single verifiable item on it.  The
>> > script
>> > collects all the links into an array, then iterates over the array,
>> > loading
>> > each link and verifying a single piece of text.  That's the easy part.
>> >
>> > Here's the relevant code for what I believe to be the closest attempt
>> > right
>> > now:
>> >
>> > @links = Array.new
>> > @browser = Watir::Browser.new
>> > @browser.goto(page)
>> >
>> > @browser.link.each do |link|
>> >    @links << [link.name, link.href]
>> > end
>> >
>> >    @links.each do |name, link|
>> >         describe name do
>> >              it "should contain the word #{name}" do
>> >                    @browser.goto link
>> >                    @browser.div(:id, /example/).text.should
>> > include(name)
>> >              end #it
>> >         end #desc
>> >    end #@links
>> >
>> > I've tried a combination of methods -- putting all of this code inside a
>> > describe block, etc, but I end up with the variables being unknown
>> > because
>> > they're not inside the same describe or it block (even when set to
>> > $global),
>> > or the describe/it blocks being layered incorrectly.
>> >
>> > I'm sure there's a simple solution to the format, but unfortunately I
>> > haven't been able to Google or RDoc my way to an answer.  I appreciate
>> > any
>> > advice you can offer!
>>
>> describe "something" do
>>  it "does something" do
>>  end
>> end
>>
>> That's the simplest format - everything should go in the example (it
>> block), not directly inside the describe block.

> Thanks David.  I have the basic format working in other scripts, so that's a
> good sign.  Another user pointed me to this example, which is why I was
> exploring a different format:
>
>>
>> ["Los Angeles", "Austin"].each do |location|
>>     it "should show #{location}" do
>>       @browser.div(:id, /example/).text.should include(location)
>>     end
>> end
>
> Is this not correct/usable?

Correct is subjective. What you have here is somewhat harmless, but
I've seen simple lists like that grow into nested lists with
conditional logic where members of one list impact what is done with
the values in the other. That makes it very difficult to understand
failures and difficult to change as requirements change.

HTH,
David


More information about the rspec-users mailing list