【问题标题】:Receiving an 'undefined local variable or method' error on a visit method, when trying to use webrat for my cucumber tests尝试使用 webrat 进行黄瓜测试时,在访问方法上收到“未定义的局部变量或方法”错误
【发布时间】:2014-11-17 18:56:52
【问题描述】:

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

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

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

【问题讨论】:

    标签: ruby-on-rails cucumber webrat


    【解决方案1】:

    我猜你想调用一个路由助手方法来生成你的登录页面的 url。

    路由辅助方法应该以 _path 或 _url 结尾。

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

    【讨论】:

    • 非常感谢,解决了这个问题,只是处理现在出现的其他错误:p 我想你不知道我能做些什么:
    • 未定义方法use_ssl' for #&lt;Rails::Application::Configuration:0x007f86f3d87198&gt; (ActionController::RoutingError) ./config/initializers/force_ssl_extension.rb:5:in force_ssl' ./app/controllers/sessions_controller.rb:2:in &lt;class:SessionsController&gt;' ./app/controllers/sessions_controller.rb:1:in ' ./features/step_definitions/user_steps.rb:10:在`/^我尝试使用我的详细信息登录$/'
    • 请为其他问题创建一个新问题并提供更多详细信息/代码。
    猜你喜欢
    • 2011-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多