Jump to content

The ultimate community for Ruby on Rails developers.


Photo

Using Test DB Data and Rails Routes with Cucumber Scenario Outlines

cucumber capybara rails 4 testing

  • Please log in to reply
2 replies to this topic

#1 Vell

Vell

    Inspector

  • Members
  • 76 posts
  • LocationWashington, DC

Posted 09 April 2014 - 06:43 PM

Hello all,

 

I am attempting to write a scenario outline in cucumber where I am passing the url location that I am either viewing or submitting data to. I have 2 issues that I have not been able to figure out. I am hoping someone can provide some suggestions on how to attack them.

 

Issue 1: Creating db data for use with example table.

In normal cucumber cases, I would have something like

Given at least 1 record is created

Which in that task I would use a factory to create that record. My question here is how do I use that created record inside of my example table where I need an ID for my route?

 

i.e. if I have a given example table:

|     login       |     location     |           action         |
| noone@yafoo.com | admin/groups     | not see "Listing groups" |
| noone@yafoo.com | admin/groups/:id | not see "Comments for"   |

On that second line where there is ':id' in the location how would I be able to use a record id that I would have created with factory girl? Currently, In my step that is processing this, I am creating the factory there and then going a sub in the location string for ':id' and replacing it with the id of the created record. This might be the right way to do it but I wanted to get a second opinion.

 

Issue 2: (and probably more important). Using the same example data above how could I replace that location data with actualy routes. Instead of using

admin/groups

 use

admin_groups_path

This would make it a lot easier for me to be able to pass IDs of records in particular case of create where I can't seem to tell the current implementation of location that I am submitting a POST as oppose to it always using GET.

I tried using URI but I didn't have much luck at all.

bad URI(is not URI?): admin_groups_path(:id) (URI::InvalidURIError)
      ./features/step_definitions/unauthorized_access_steps.rb:14:in `/^"(.*?)" is visited$/'
      features/unauthorized_access.feature:10:in `When "<location>" is visited'

I am wondering if anyone else has tried to do this. If so, how you were able to get this to work.

 

Thanks in advance for any suggestions.



#2 stevieing

stevieing

    Dispatcher

  • Members
  • 40 posts

Posted 10 April 2014 - 07:53 AM

1. use an instance variable. Even better use a helper method. In your helper file you can have a method like so:

 

def current_record

 @record ||= create(:record)

end

 

You can then use current_record across the steps of your scenario.

 

2. There is a tried and tested way for managing paths.

 

In your scenario:

When I go to the admin groups page.

 

In your step:

When /^I go to (.*)$ do |page_name|

  visit path_to(page_name)

end

 

In your helper:

def path_to(page_name)

  case page_name

  when /the home\s?page/

   '/'

   else

     begin

       page_name =~ /the (.*) page/

       path_components = $1.split(/\s+/)

       self.send(path_components.push('path').join('_'))

     rescue Object => e

       raise 'Can't find mapping from \"#{page_name}\" to a path.\n" +

          "Now, go and add a mapping in #{__FILE__}"

     end

   end

  end

end

 

This is a reusable method which can be used for all of your paths in which you can use case statements for special cases.

 

Another piece of advice - buy the Cucumber book, it's excellent. http://pragprog.com/...e-cucumber-book. It will change the way you write your tests.

 

I will leave you to work out how to put the two together! which would be a combination of the step and the path_to method.

 

Hope that helps

 

Steve.



#3 Vell

Vell

    Inspector

  • Members
  • 76 posts
  • LocationWashington, DC

Posted 17 April 2014 - 12:48 PM

I read the book (just finished yesterday). Was very insightful on how to test using cucumber. Specially not using instance variables in the step definitions but move them to a helper method.  In the cucumber group, I was told the way I was using scenario outline was a good example of how not use it. So I changed it and broke out to individual tests. There is still a ton of feature duplication which is what I was attemping to avoid by using scenario outline so I will have to re-read the chapter on Gherkin since I am pretty sure I read something about that specifically.







Also tagged with one or more of these keywords: cucumber, capybara, rails 4, testing

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users