views:

261

answers:

3

I am trying out Cucumber for the first time and I've come accross an issue. I am try to use RyanB's nifty authentication generator, but it seems I can't use the helper methods when running my Cucumber features.

Part of my feature giving me problems:

when I am on the new book page                                   # features/step_definitions/web_steps.rb:18
      undefined local variable or method `current_user' for #<Cucumber::Rails::World:0x13a3b2a> (NameError)
      ./features/support/paths.rb:15:in `path_to'
      ./features/step_definitions/web_steps.rb:19:in `/^(?:|I )am on (.+)$/'
      features/user_story.feature:30:in `When I am on the new book page'

support/paths.rb :

when /the new book page/
  user_path(current_user)+'/books/new'

routes.rb :

resources :users do
  resources :books
end

The piece of code that is giving me my authentication helper methods (like current_user) is inside my lib/ folder. I've tried:

require "#{Rails.root}/lib/authentication"

In my env.rb, but that doesn't seem to do anything. So what should I be doing here instead? I feel there should be a simple solution to this, but it's just going over my head.

Extra Info

gem env :

RubyGems Environment:
  - RUBYGEMS VERSION: 1.3.6
  - RUBY VERSION: 1.9.2 (2010-03-04 patchlevel -1) [i386-darwin9.8.0]
  - INSTALLATION DIRECTORY: /Users/ekoslow/.rvm/gems/ruby-1.9.2-head%rails3beta
  - RUBY EXECUTABLE: /Users/ekoslow/.rvm/rubies/ruby-1.9.2-head/bin/ruby
  - EXECUTABLE DIRECTORY: /Users/ekoslow/.rvm/gems/ruby-1.9.2-head%rails3beta/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86-darwin-9
  - GEM PATHS:
     - /Users/ekoslow/.rvm/gems/ruby-1.9.2-head%rails3beta
     - /Users/ekoslow/.rvm/gems/ruby-1.9.2-head%global
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :benchmark => false
     - :backtrace => false
     - :bulk_threshold => 1000
     - "gem" => "--no-ri --no-rdoc"
     - :sources => ["http://gems.rubyforge.org", "http://gems.github.com"]
  - REMOTE SOURCES:
     - http://gems.rubyforge.org
     - http://gems.github.com

And I'm running Rails 3.0.0.beta, but I don't think that has to do with anything in this case.

A: 

current_user is define only in your Controller context. In your cucumber spec you have only externe data.

A good cucumber test click on button, not go directly on URL. So you do need know how is this URL or where you can click to go to.

shingara
+2  A: 

Presumably your application requires you to be logged in to view the new book page.

If that is the case then "Being logged" in is a pre-requisite for this test so should be in your Givens.

So start the test with Given I am logged in and use the I am logged in code to define current_user

Steve Weet
Ok, thats not what I was going for, but I guess creating an instance variable isn't going to kill me.
Eric Koslow
A: 

Don't create an instance variable

Log in using something like http://www.francisfish.com/debugging_cucumber_scripts_cucumber_and_devise_authenticati.htm

(scroll down to the bit about the devise plugin)

This example code is for the devise gem but all you need to do is fill in the right fields on the login page.

This makes you exercise the GUI and be a client to the app.

Ghoti