我正在运行 RSpec 测试,但收到一条错误消息:undefined local variable or method `applicant_signup_path'。这很奇怪,因为申请 者签名路径在测试的其他地方工作)
当我追踪错误时,它看起来与 rspec-expectations, method_missing.rb:9:in `method_missing' 有某种关联
jyrki@ubu:~/rails_projects/p4$ bundle exec rspec -b spec/requests/applicant_signup_spec.rb
......F.....
Failures:
1) ApplicantSignup signup with valid information after email URL validation, part I with the right activation key
Failure/Error: before { visit applicant_signup_path }
NameError:
undefined local variable or method `applicant_signup_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_2::Nested_2::Nested_2::Nested_1:0xbb88fac>
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-expectations-2.13.0/lib/rspec/matchers/method_missing.rb:9:in `method_missing'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/testing/assertions/routing.rb:171:in `method_missing'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/actionpack-4.0.2/lib/action_dispatch/testing/integration.rb:398:in `method_missing'
# ./spec/requests/applicant_signup_spec.rb:15:in `block (3 levels) in <top (required)>'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:237:in `instance_eval'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:237:in `instance_eval'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:21:in `run'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:66:in `block in run'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:66:in `each'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:66:in `run'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:418:in `run_hook'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:334:in `run_before_each_hooks'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:300:in `run_before_each'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:113:in `block in run'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:179:in `call'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:179:in `run'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_exec'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/extensions/instance_eval_with_args.rb:16:in `instance_eval_with_args'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:247:in `instance_eval_with_args'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:87:in `block (2 levels) in run'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:89:in `call'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:89:in `run'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/hooks.rb:418:in `run_hook'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:329:in `run_around_each_hooks'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:256:in `with_around_each_hooks'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/example.rb:111:in `run'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:390:in `block in run_examples'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:386:in `map'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:386:in `run_examples'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:371:in `run'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `block in run'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `map'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `run'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `block in run'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `map'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `run'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `block in run'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `map'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `run'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `block in run'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `map'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/example_group.rb:372:in `run'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:28:in `map'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:28:in `block in run'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/reporter.rb:34:in `report'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/command_line.rb:25:in `run'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:80:in `run'
# /home/jyrki/.rvm/gems/ruby-2.1.0/gems/rspec-core-2.13.1/lib/rspec/core/runner.rb:17:in `block in autorun'
Finished in 1.78 seconds
12 examples, 1 failure
Failed examples:
rspec ./spec/requests/applicant_signup_spec.rb:77 # ApplicantSignup signup with valid information after email URL validation, part I with the right activation key
我的 Ruby 版本是 2.1.0,Gemfile 在下面
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '4.0.2'
gem 'bootstrap-sass', '2.0.0'
gem 'bcrypt-ruby', '3.1.2'
# state machine
gem 'state_machine', '1.2.0'
# Use PostgreSQL as the database for Active Record
gem 'pg', '~> 0.17.0'
group :development, :test do
gem 'rspec-rails', '2.13.1' #'2.11.0'
#gem 'minitest', '~> 5.3.1'
end
group :development do
gem 'guard-rspec', '0.5.5'
gem "annotate", "~> 2.4.1.beta"
end
group :assets do
# Use SCSS for stylesheets
gem 'sass-rails', '~> 4.0.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '~> 2.4.0'
# Use CoffeeScript for .js.coffee assets and views
gem 'coffee-rails', '~> 4.0.0'
end
group :test do
gem 'capybara', '~> 1.1.2'
gem 'rb-inotify', '~> 0.9.0'
gem 'libnotify', '0.5.9'
gem 'factory_girl_rails', '4.2.0'
end
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use jquery as the JavaScript library
gem 'jquery-rails', '~> 3.0.0'
# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks
gem 'turbolinks', '~> 2.2.0'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.0.0'
group :doc do
# bundle exec rake doc:rails generates the API under doc/api.
gem 'sdoc', '0.4.0', require: false
end
Routes 文件(文件的开头)
P4::Application.routes.draw do
resources :applicants
resources :applicant_sessions, only: [:new, :create, :destroy]
resources :employers
resources :employer_sessions, only: [:new, :create, :destroy]
root to: 'static_pages#home'
match '/applicant_signup', to: 'applicants#new', via: 'get'
match '/applicant_signin', to: 'applicant_sessions#new', via: 'get'
match '/applicant_signout', to: 'applicant_sessions#destroy', via: 'delete'
match '/employer_signup', to: 'employers#new', via: 'get'
match '/employer_signin', to: 'employer_sessions#new', via: 'get'
match '/employer_signout', to: 'employer_sessions#destroy', via: 'delete'
创建错误的测试文件申请人_signup_spec.rb
require 'spec_helper'
describe "ApplicantSignup" do
subject { page }
describe "signup page" do
before { visit applicant_signup_path }
it { should have_selector( 'h1', text: 'Sign up') }
it { should have_selector('title', text: full_title('Sign up')) }
end
describe "signup" do
before { visit applicant_signup_path } #this creates the problem but not on the first run
let(:submit) { "Create my account"}
describe "with invalid information" do
it "should not create an applicant" do
expect { click_button submit }.not_to change(Applicant, :count)
end
end
describe "with valid information" do
#let(:applicant) { create_random_applicant }
before do
@email_random_part = Random.new.rand(10000..99999999).to_s
@email = "bart@test" + @email_random_part + ".sun"
fill_in "First name", with: "Bart"
fill_in "Last name", with: "Simpson"
fill_in "Email", with: @email
fill_in "Password", with: "foobar"
fill_in "Password confirmation", with: "foobar"
end
it "should create an applicant" do
expect { click_button submit }.to change(Applicant, :count).by(1)
end
#Welcome page
describe "welcome page" do
before { click_button submit }
it { should have_selector('title', text: "Welcome") }
#Name
#it { should have_content( "Bart") }
it { should_not have_content( "Sign out") }
it { Applicant.find_by_email(@email).can_activate?.should eq(true) }
end
#after email URL validation
describe "after email URL validation, part I" do
before { click_button submit } #sign_up first
describe "with the right activation key" do
let(:app) { Applicant.find_by_email(@email) }
let(:new_app) { NewApplicant.find_by_applicant_id!(app.id) }
before do
get "/validate_app", :validate_app => {:key => new_app.activation_key }
#visit validate_app_path
#app_sign_in app
end
#The line below creates the error#
it { new_app.activation_key.should_not eq(nil) } #Error, Error `method_missing' !!
end
end
end
describe "with valid information, part 2" do
#let(:applicant) { create_random_applicant }
before do
@email_random_part2 = Random.new.rand(10000..99999999).to_s
@email2 = "bart@test" + @email_random_part2 + ".sun"
fill_in "First name", with: "Bart"
fill_in "Last name", with: "Simpson"
fill_in "Email", with: @email2
fill_in "Password", with: "foobar"
fill_in "Password confirmation", with: "foobar"
end
it "should create an applicant" do
expect { click_button submit }.to change(Applicant, :count).by(1)
end
#after email URL validation
describe "after email URL validation, part II " do
before { click_button submit } #sign_up first
describe "with the wrong activation key" do
let(:app2) { Applicant.find_by_email(@email2) }
let(:new_app2) { NewApplicant.find_by_applicant_id!(app2.id) }
before do
visit validate_app_path, :key => "wrong-key"
end
it { should have_selector('title', text: "Error") }
it { should have_content( "Error") }
it { Applicant.find_by_id(app2.id).can_disable?.should eq(false) }
end
end
end
end
end