How to write a step for a feature with a select control

I was wondering what the correct procedure would be for spec’ing the
following story line

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?

Damian J. wrote:

I was wondering what the correct procedure would be for spec’ing the
following story line

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.

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

HTH

Joseph W.
http://www.joesniff.co.uk

Hi Damian,

On Tue, Sep 16, 2008 at 6:43 AM, Joseph W. [email protected]
wrote:

Damian J. wrote:

I was wondering what the correct procedure would be for spec’ing the
following story line

First, a bit of remedial terminology :wink:

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.”

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)
end

  1. 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
end

The “selects option_value …” statement is using Webrat, in case
you’re not familiar.

HTHT,
David

Hi David,

Once again, thanks, that is exactly what I was looking for.
Thanks for clearing up the terminology also, I was hoping someone would
do that.

Cheers,
Damian

David C. wrote:

  1. 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
end

HI David, I tried the method above but I am getting “You have a nil
object when you didn’t expect it”

I can see that the following line is creating a new ArticleCategory with
:name => “My article category” (BTW is it just coincidence that I have
the field :name in my Article category table or is this a convention I
should be following anyway?)

field_name.gsub(’ ‘,’’).constantize.find_or_create_by_name(option_value)

But I don’t see where this new ArticleCategory is getting bound to the
select control.

Sorry David,
I don’t really understand.

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
end

field_name.gsub(’ ‘,’’).constantize.find_or_create_by_name(option_value)
– This line creates a new ArticleCategory, but where does that
ArticleCategory go, or how is it used?

selects option_value, :from => field_name
– Then this tries to select that ArticleCategory from the select
control?

On Tue, Sep 16, 2008 at 9:55 AM, Damian J. [email protected]
wrote:

HI David, I tried the method above but I am getting “You have a nil
object when you didn’t expect it”

I can see that the following line is creating a new ArticleCategory with
:name => “My article category” (BTW is it just coincidence that I have
the field :name in my Article category table or is this a convention I
should be following anyway?)

It is a convention that I tend to follow, but I wouldn’t say that it’s
a community wide convention, nor would I necessarily recommend it as
such. I’ll just say it works well for me.

What I would recommend is that you use some convention when it comes
to things that are going to show up in lists :slight_smile:

field_name.gsub(’ ‘,’').constantize.find_or_create_by_name(option_value)

But I don’t see where this new ArticleCategory is getting bound to the
select control.

That should be the responsibility of your application code - something
you would drive out with code examples rather than scenarios.

Make sense?

David

OK,

I managed to get it to work using David’s option 1.

  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)
end

My Scenario now goes:

Given an Article Category named “My Article Category”
And I am on the new article page
When I fill in “Title” with “My article title”
And I fill in “Summary” with “My article summary”
And I fill in “Body” with “My article body”
And I check “Is Live”
And I pick “My Article Category” from Article Category
And I press “Create”
Then I should see “My article title”
And I should see “My article summary”
And I should see “My article body”
And I should see “Article is live”
And I should see “My Article Category”

And that works. I could not get it to work using David’s option 2

  1. 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
end

Have I broken any rules, or is that OK?

On Tue, Sep 16, 2008 at 2:00 PM, Damian J. [email protected]
wrote:

And I press “Create”
Then I should see “My article title”
And I should see “My article summary”
And I should see “My article body”
And I should see “Article is live”
And I should see “My Article Category”

And that works. I could not get it to work using David’s option 2

That’s a matter of order of events. The html is loaded when you say
“And I am on the new article page” so it’s too late to add new data
for display on that page. Make sense?

  1. 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
end

Have I broken any rules, or is that OK?

This is OK, but falls under what some call an imperative style (as
opposed to declarative). While it can be useful to have a scenario or
two for any given feature that goes field by field, the declarative
style helps you keep things a bit more expressive, more high level,
less verbose, and less brittle.

Check out these resources:

http://blog.davidchelimsky.net/assets/2008/7/14/IntegrationTestingWithRSpec.pdf.zip
http://www.brynary.com/uploads/Story_Driven_Development.pdf

Cheers,
David

Check out these resources:

http://blog.davidchelimsky.net/assets/2008/7/14/IntegrationTestingWithRSpec.pdf.zip
http://www.brynary.com/uploads/Story_Driven_Development.pdf

Thanks David,

They are excellent resources, cleared up a lot of things for me.

Damian

I think they could do with creeping onto the rspec.info pages too.
Those pages are pretty thin for a stories newbie.

On 17 Sep 2008, at 09:22, Damian J. wrote:

They are excellent resources, cleared up a lot of things for me.

Damian

Posted via http://www.ruby-forum.com/.


rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users

cheers,
Matt

http://blog.mattwynne.net

In case you wondered: The opinions expressed in this email are my own
and do not necessarily reflect the views of any former, current or
future employers of mine.

To follow on from David’s great links, I posted an article recently
which has a collection of links that I found very useful while learning
stories. It also has some of the lessons I learnt along the way. Thought
it might be helpful.

http://www.joesniff.co.uk/ruby/telling-a-good-story-rspec-stories-from-the-trenches.html


Joseph W.
http://www.joesniff.co.uk

Matt W. wrote:

I think they could do with creeping onto the rspec.info pages too.
Those pages are pretty thin for a stories newbie.

On 17 Sep 2008, at 09:22, Damian J. wrote:

They are excellent resources, cleared up a lot of things for me.

Damian

Posted via http://www.ruby-forum.com/.


rspec-users mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/rspec-users

cheers,
Matt

http://blog.mattwynne.net
http://songkick.com

In case you wondered: The opinions expressed in this email are my own
and do not necessarily reflect the views of any former, current or
future employers of mine.