在尝试使用webrat进行黄瓜测试时,在访问方法上接收“未定义的局部变量或方法”错误

时间:2014-11-13 16:25:45

标签: ruby-on-rails cucumber webrat

我已经检查了所有以前的问题和整个谷歌的答案,但仍未解决。

我在运行黄瓜测试时收到以下错误:

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

这是测试的结果:

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

访问new_session失败

以下是我的env.rb文件中的所有内容(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

这是我的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

这是user_steps.rb文件(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

以下是我的路线:

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

1 个答案:

答案 0 :(得分:1)

我想你想调用一个路由助手方法,它会为你的登录页面生成url。

路由助手方法应以_path或_url。

结尾

因此,您应该在步骤定义中将new_session替换为new_session_path。