[rspec-users] How to write a step for a feature with a select control

David Chelimsky dchelimsky at gmail.com
Tue Sep 16 09:05:56 EDT 2008

Hi Damian,

On Tue, Sep 16, 2008 at 6:43 AM, Joseph Wilk <lists at ruby-forum.com> wrote:
> Damian Jones wrote:
>> I was wondering what the correct procedure would be for spec'ing the
>> following story line

First, a bit of remedial terminology ;)

For better or worse, we generally use "spec'ing" to describe driving
out application code with code examples.

What you're calling a "story line" is simply a "step."

The thing you're looking for (the correct procedure) is a "step definition."

>> When I pick "My Option" from "My Field"
>> "My Field" would be an Article Category select control, when running my
>> feature the control does not have any data bound to it.
>> I am guessing I would somehow bind the control to some data in the
>> corresponding Step
>> Am I going along the right lines, or can someone enlighten me with a
>> better way of doing this?
> Sounds right. You could use either a string in your when step.

... step definition ...

> When("I pick '$option' from my '$field'") do |option, field|
> end
> OR you could use a regular expression
> When(/^I pick '(.*?)' from my '(.*?)'$/) do |option, field|
> end

I think Damian is looking for what to do in the step definition, not
necessarily the expression to match it.

Damian - I take it that Article Categories are themselves data driven,
yes? If so, I'd do one of two things:

1. add the data in a previous step

Given /an? (.*) named "(.*)"$/ do |class_name, object_name|
  class_name.gsub(' ','').constantize.find_or_create_by_name(object_name)

2. add the data in the current step

When(/^I pick "(.*)" from (.*)$/) do |option_value, field_name|
  field_name.gsub(' ','').constantize.find_or_create_by_name(option_value)
  selects option_value, :from => field_name

The "selects option_value ..." statement is using Webrat, in case
you're not familiar.


> --
> Joseph Wilk
> http://www.joesniff.co.uk

More information about the rspec-users mailing list