Search code examples
ruby-on-railscucumberwebrat

Receiving an 'undefined local variable or method' error on a visit method, when trying to use webrat for my cucumber tests


I have checked all previous questions and the whole of google for an answer to this, but still not solved.

I am receiving the following error when running my cucumber tests:

ps.rb:1
    When I attempt to login with my details 
    # features/step_definitions/user_steps.rb:7
    undefined local variable or method `new_session' for #<Cucumber::Rails::World:0x007ff5943e0620> (NameError)
    ./features/step_definitions/user_steps.rb:8:in 
    /^I attempt to login with my details$/
    features/users/login.feature:8:in `When I attempt to login with my details

And here is the outcome of the tests:

Failing Scenarios:
cucumber features/users/login.feature:6 
# Scenario: User does not have an account

6 scenarios (1 failed, 5 undefined)
30 steps (1 failed, 6 skipped, 22 undefined, 1 passed)
0m0.763s

It is failing at visit new_session

Here is everything in my env.rb file (features/support/env.rb):

require 'cucumber/rails'
require 'webrat'
require 'webrat/core/matchers'

Webrat.configure do |config|
  config.mode = :rack
  config.open_error_files = false # Set to true if you want error pages to pop up in the browser
end

ActionController::Base.allow_rescue = false

# Remove/comment out the lines below if your app doesn't have a database.
# For some databases (like MongoDB and CouchDB) you may need to use :truncation instead.
begin
  DatabaseCleaner.strategy = :transaction
rescue NameError
  raise "You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it."
end

# Possible values are :truncation and :transaction
# The :transaction strategy is faster, but might give you threading problems.
# See https://github.com/cucumber/cucumber-rails/blob/master/features/choose_javascript_database_strategy.feature
Cucumber::Rails::Database.javascript_strategy = :truncation

Here is my Gemfile:

gem 'thin'

gem 'paperclip', '~> 4.1'

gem 'simple-navigation'

gem 'will_paginate', '~> 3.0'

# Required by Windows and some Linux platforms when running with Rails 4.1.5!
gem 'tzinfo-data'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.1.5'

# Use sqlite3 as the database for Active Record
gem 'sqlite3'

# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.3'

# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'

# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'

gem 'oauth', '~>0.4.6'

# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem "jquery-rails", "~> 2.3.0"
gem 'jquery-ui-rails', '4.1.2'

# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks'

# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 1.2'

group :doc do
  # bundle exec rake doc:rails generates the API under doc/api.
  gem 'sdoc', require: false
end

group :development, :test do
  gem 'rspec-rails', '~> 3.0.0'
  gem 'cucumber-rails', :require => false 
  gem "webrat"
  gem 'factory_girl_rails'
  gem 'capybara'
  gem 'database_cleaner'
end

Here is the user_steps.rb file (features/step_definitons/user_steps.rb):

Given /^I do not have an account$/ do
    @user = FactoryGirl.create(:user)
    @user ||= User.where(:email >= @user[:email]).first
    @user.destroy unless @user.nil?
end

When /^I attempt to login with my details$/ do
    visit new_session
    fill_in "login", :with => "invalid_login" 
    fill_in "password", :with => "invalid_password" 
    click_button "commit"
end 

Then /^I see a login error message$/ do 
    response.should contain("Couldn't log you in as invalid_login")
end

And /^I should not be logged in$/ do
    visit home
    response.should_not contain("Welcome admin")
end

Here is the feature, in the login.features file (features/users/login.feature):

Feature: Login
    In order to access my account 
    As a user
    I want to be able to login 

        Scenario: User does not have an account
            Given I do not have an account
            When I attempt to login with my details 
            Then I see a login error message
            And I should not be logged in

Here are my routes:

Prefix Verb   URI Pattern               Controller#Action
 search_users GET    /users/search(.:format)   users#search
        users GET    /users(.:format)          users#index
              POST   /users(.:format)          users#create
     new_user GET    /users/new(.:format)      users#new
    edit_user GET    /users/:id/edit(.:format) users#edit
         user GET    /users/:id(.:format)      users#show
              PATCH  /users/:id(.:format)      users#update
              PUT    /users/:id(.:format)      users#update
              DELETE /users/:id(.:format)      users#destroy
   broadcasts GET    /broadcasts(.:format)     broadcasts#index
              POST   /broadcasts(.:format)     broadcasts#create
new_broadcast GET    /broadcasts/new(.:format) broadcasts#new
    broadcast GET    /broadcasts/:id(.:format) broadcasts#show
              DELETE /broadcasts/:id(.:format) broadcasts#destroy
      session POST   /session(.:format)        sessions#create
  new_session GET    /session/new(.:format)    sessions#new
              DELETE /session(.:format)        sessions#destroy
         home GET    /home(.:format)           home#index
         root GET    /                         home#index

Solution

  • I guess you want to call a route helper method which would generate the url to your login page.

    Route helper methods should end with _path or _url.

    So you should replace new_session with new_session_path in your step definitions.