使用 Rspec/Capybara 时找不到 Rails 路线,[Rails 3.1.1,Ruby 1.9.3]
你好,我正在 OSX Lion 上使用 rspec、capybara、guard、spork 和 Rails 3.1.1 和 ruby 1.9.3 开发一个 Rails 项目,
我有以下测试
context "As a signed in user" do
let(:user) { FactoryGirl.create(:user) }
before(:each) do
visit new_user_session_path
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
end
describe "creating categories" do
it "helps me to create a new category" do
# given
visit new_category_path
# when
fill_in "Name", with: 'Supermarket'
fill_in "Color", with: "#CCC"
click_button "New Category"
# then
current_path.should eq(categories_path)
page.should have_content("Category successfully created")
category = user.categories.last
category.name.should eq("Supermarket")
category.color.should eq("#CCC")
end
end
end
,但是当它运行时,我收到以下错误
Categories As a signed in user creating categories helps me to create a new category
Failure/Error: visit new_category_path
NameError:
undefined local variable or method `new_category_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1::Nested_1:0x007fff0dcdccb0>
但是我的输出 < code>rake paths 命令是
categories GET /categories(.:format) {:action=>"index", :controller=>"categories"}
POST /categories(.:format) {:action=>"create", :controller=>"categories"}
new_category GET /categories/new(.:format) {:action=>"new", :controller=>"categories"}
edit_category GET /categories/:id/edit(.:format) {:action=>"edit", :controller=>"categories"}
category GET /categories/:id(.:format) {:action=>"show", :controller=>"categories"}
PUT /categories/:id(.:format) {:action=>"update", :controller=>"categories"}
DELETE /categories/:id(.:format) {:action=>"destroy", :controller=>"categories"}
category_index GET /category/index(.:format) {:controller=>"category", :action=>"index"}
category_new GET /category/new(.:format) {:controller=>"category", :action=>"new"}
welcome_index GET /welcome/index(.:format) {:controller=>"welcome", :action=>"index"}
new_user_session GET /users/sign_in(.:format) {:action=>"new", :controller=>"devise/sessions"}
user_session POST /users/sign_in(.:format) {:action=>"create", :controller=>"devise/sessions"}
destroy_user_session DELETE /users/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"}
user_password POST /users/password(.:format) {:action=>"create", :controller=>"devise/passwords"}
new_user_password GET /users/password/new(.:format) {:action=>"new", :controller=>"devise/passwords"}
edit_user_password GET /users/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
PUT /users/password(.:format) {:action=>"update", :controller=>"devise/passwords"}
cancel_user_registration GET /users/cancel(.:format) {:action=>"cancel", :controller=>"devise/registrations"}
user_registration POST /users(.:format) {:action=>"create", :controller=>"devise/registrations"}
new_user_registration GET /users/sign_up(.:format) {:action=>"new", :controller=>"devise/registrations"}
edit_user_registration GET /users/edit(.:format) {:action=>"edit", :controller=>"devise/registrations"}
PUT /users(.:format) {:action=>"update", :controller=>"devise/registrations"}
DELETE /users(.:format) {:action=>"destroy", :controller=>"devise/registrations"}
user_confirmation POST /users/confirmation(.:format) {:action=>"create", :controller=>"devise/confirmations"}
new_user_confirmation GET /users/confirmation/new(.:format) {:action=>"new", :controller=>"devise/confirmations"}
GET /users/confirmation(.:format) {:action=>"show", :controller=>"devise/confirmations"}
root / {:controller=>"welcome", :action=>"index"}
所以我不知道出了什么问题,如果我注释掉访问 new_user_session_path 它与categories_path有相同的错误
我希望有人可以帮助我这个问题,谢谢!
顺便说一句,这是我的 spec_helper.rb
require 'rubygems'
require 'spork'
Spork.prefork do
# Loading more in this block will cause your tests to run faster. However,
# if you change any configuration or code from libraries loaded here, you'll
# need to restart spork for it take effect.
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
# == Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
config.mock_with :rspec
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
config.treat_symbols_as_metadata_keys_with_true_values = true
config.filter_run :focus => true
config.run_all_when_everything_filtered = true
end
end
Spork.each_run do
# This code will be run each time you run your specs.
FactoryGirl.reload
end
# --- Instructions ---
# - Sort through your spec_helper file. Place as much environment loading
# code that you don't normally modify during development in the
# Spork.prefork block.
# - Place the rest under Spork.each_run block
# - Any code that is left outside of the blocks will be ran during preforking
# and during each_run!
# - These instructions should self-destruct in 10 seconds. If they don't,
# feel free to delete them.
#
Hi I'm working on a rails project with rspec, capybara, guard, spork with rails 3.1.1 and ruby 1.9.3 on OSX Lion
I have the following test
context "As a signed in user" do
let(:user) { FactoryGirl.create(:user) }
before(:each) do
visit new_user_session_path
fill_in "Email", with: user.email
fill_in "Password", with: user.password
click_button "Sign in"
end
describe "creating categories" do
it "helps me to create a new category" do
# given
visit new_category_path
# when
fill_in "Name", with: 'Supermarket'
fill_in "Color", with: "#CCC"
click_button "New Category"
# then
current_path.should eq(categories_path)
page.should have_content("Category successfully created")
category = user.categories.last
category.name.should eq("Supermarket")
category.color.should eq("#CCC")
end
end
end
But when it runs I get the following error
Categories As a signed in user creating categories helps me to create a new category
Failure/Error: visit new_category_path
NameError:
undefined local variable or method `new_category_path' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1::Nested_1:0x007fff0dcdccb0>
But My output of the rake routes
command is
categories GET /categories(.:format) {:action=>"index", :controller=>"categories"}
POST /categories(.:format) {:action=>"create", :controller=>"categories"}
new_category GET /categories/new(.:format) {:action=>"new", :controller=>"categories"}
edit_category GET /categories/:id/edit(.:format) {:action=>"edit", :controller=>"categories"}
category GET /categories/:id(.:format) {:action=>"show", :controller=>"categories"}
PUT /categories/:id(.:format) {:action=>"update", :controller=>"categories"}
DELETE /categories/:id(.:format) {:action=>"destroy", :controller=>"categories"}
category_index GET /category/index(.:format) {:controller=>"category", :action=>"index"}
category_new GET /category/new(.:format) {:controller=>"category", :action=>"new"}
welcome_index GET /welcome/index(.:format) {:controller=>"welcome", :action=>"index"}
new_user_session GET /users/sign_in(.:format) {:action=>"new", :controller=>"devise/sessions"}
user_session POST /users/sign_in(.:format) {:action=>"create", :controller=>"devise/sessions"}
destroy_user_session DELETE /users/sign_out(.:format) {:action=>"destroy", :controller=>"devise/sessions"}
user_password POST /users/password(.:format) {:action=>"create", :controller=>"devise/passwords"}
new_user_password GET /users/password/new(.:format) {:action=>"new", :controller=>"devise/passwords"}
edit_user_password GET /users/password/edit(.:format) {:action=>"edit", :controller=>"devise/passwords"}
PUT /users/password(.:format) {:action=>"update", :controller=>"devise/passwords"}
cancel_user_registration GET /users/cancel(.:format) {:action=>"cancel", :controller=>"devise/registrations"}
user_registration POST /users(.:format) {:action=>"create", :controller=>"devise/registrations"}
new_user_registration GET /users/sign_up(.:format) {:action=>"new", :controller=>"devise/registrations"}
edit_user_registration GET /users/edit(.:format) {:action=>"edit", :controller=>"devise/registrations"}
PUT /users(.:format) {:action=>"update", :controller=>"devise/registrations"}
DELETE /users(.:format) {:action=>"destroy", :controller=>"devise/registrations"}
user_confirmation POST /users/confirmation(.:format) {:action=>"create", :controller=>"devise/confirmations"}
new_user_confirmation GET /users/confirmation/new(.:format) {:action=>"new", :controller=>"devise/confirmations"}
GET /users/confirmation(.:format) {:action=>"show", :controller=>"devise/confirmations"}
root / {:controller=>"welcome", :action=>"index"}
So I don't know what is wrong, and If I comment out the visit new_user_session_path it has the same error with categories_path
I hope someone can help me with this problem, Thanks!
BTW here's my spec_helper.rb
require 'rubygems'
require 'spork'
Spork.prefork do
# Loading more in this block will cause your tests to run faster. However,
# if you change any configuration or code from libraries loaded here, you'll
# need to restart spork for it take effect.
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
RSpec.configure do |config|
# == Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
config.mock_with :rspec
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false
config.treat_symbols_as_metadata_keys_with_true_values = true
config.filter_run :focus => true
config.run_all_when_everything_filtered = true
end
end
Spork.each_run do
# This code will be run each time you run your specs.
FactoryGirl.reload
end
# --- Instructions ---
# - Sort through your spec_helper file. Place as much environment loading
# code that you don't normally modify during development in the
# Spork.prefork block.
# - Place the rest under Spork.each_run block
# - Any code that is left outside of the blocks will be ran during preforking
# and during each_run!
# - These instructions should self-destruct in 10 seconds. If they don't,
# feel free to delete them.
#
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是我没有运行 rake db:test:prepare 命令:P
the problem was that I hadn't run the rake db:test:prepare command :P
我刚刚遇到了这个问题,但我失败的原因是我不小心将测试代码放入了没有
it
块的describe
块中。基本上:当它应该是
I just ran into this issue, but the reason mine was failing was that I was accidentally putting my test code into a
describe
block with noit
block. Basically:when it should have been