Damian Jones
2008-Sep-16 10:06 UTC
[rspec-users] 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? -- Posted via http://www.ruby-forum.com/.
Joseph Wilk
2008-Sep-16 11:43 UTC
[rspec-users] How to write a step for a feature with a select control
Damian Jones 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 Wilk http://www.joesniff.co.uk -- Posted via http://www.ruby-forum.com/.
David Chelimsky
2008-Sep-16 13:05 UTC
[rspec-users] How to write a step for a feature with a select control
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 lineFirst, 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| > endI 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 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 end The "selects option_value ..." statement is using Webrat, in case you''re not familiar. HTHT, David> HTH > -- > Joseph Wilk > http://www.joesniff.co.uk
Damian Jones
2008-Sep-16 13:34 UTC
[rspec-users] How to write a step for a feature with a select control
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 -- Posted via http://www.ruby-forum.com/.
Damian Jones
2008-Sep-16 14:55 UTC
[rspec-users] How to write a step for a feature with a select control
David Chelimsky wrote:> 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 > endHI 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. -- Posted via http://www.ruby-forum.com/.
David Chelimsky
2008-Sep-16 15:06 UTC
[rspec-users] How to write a step for a feature with a select control
On Tue, Sep 16, 2008 at 9:55 AM, Damian Jones <lists at ruby-forum.com> wrote:> David Chelimsky wrote: > >> 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 >> 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?)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 :)> 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
Damian Jones
2008-Sep-16 15:39 UTC
[rspec-users] How to write a step for a feature with a 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? -- Posted via http://www.ruby-forum.com/.
Damian Jones
2008-Sep-16 19:00 UTC
[rspec-users] How to write a step for a feature with a select control
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) > endMy 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> 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 > endHave I broken any rules, or is that OK? -- Posted via http://www.ruby-forum.com/.
David Chelimsky
2008-Sep-16 19:09 UTC
[rspec-users] How to write a step for a feature with a select control
On Tue, Sep 16, 2008 at 2:00 PM, Damian Jones <lists at ruby-forum.com> wrote:> 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 2That''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?>> 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 >> 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> -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
Damian Jones
2008-Sep-17 08:22 UTC
[rspec-users] How to write a step for a feature with a select control
> Check out these resources: > > http://blog.davidchelimsky.net/assets/2008/7/14/IntegrationTestingWithRSpec.pdf.zip > http://www.brynary.com/uploads/Story_Driven_Development.pdfThanks David, They are excellent resources, cleared up a lot of things for me. Damian -- Posted via http://www.ruby-forum.com/.
Matt Wynne
2008-Sep-17 09:38 UTC
[rspec-users] How to write a step for a feature with a select control
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 Jones wrote:> >> 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 > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-userscheers, 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.
Joseph Wilk
2008-Sep-17 12:10 UTC
[rspec-users] How to write a step for a feature with a select control
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 Wilk http://www.joesniff.co.uk Matt Wynne 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 Jones 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 >> rspec-users at rubyforge.org >> 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.-- Posted via http://www.ruby-forum.com/.