使用Rspec/Capybara时未找到Rails路由,[Rails 3.1.1,Ruby 1.9.3]
大家好,我正在与rspec、capybara、guard、spork合作一个rails项目,在OSX Lion上使用rails 3.1.1和ruby 1.9.3 我有以下测试使用Rspec/Capybara时未找到Rails路由,[Rails 3.1.1,Ruby 1.9.3],rspec,ruby-on-rails-3.1,routes,capybara,Rspec,Ruby On Rails 3.1,Routes,Capybara,大家好,我正在与rspec、capybara、guard、spork合作一个rails项目,在OSX Lion上使用rails 3.1.1和ruby 1.9.3 我有以下测试 context "As a signed in user" do let(:user) { FactoryGirl.create(:user) } before(:each) do visit new_user_session_path fill_in "Email",
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>
因此,我不知道是什么问题,如果我注释掉访问新用户会话路径,它与类别路径有相同的错误
我希望有人能帮我解决这个问题,谢谢
顺便说一句,这是我的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.
#
问题是我没有运行rake db:test:prepare命令:p我刚刚遇到了这个问题,但我失败的原因是我不小心将我的测试代码放入了一个
descripe
块,而没有it
块。基本上:
describe "Add Post" do
visit post_path(@post)
end
应该是什么时候
describe "Add Post" do
it "shows the Post" do
visit post_path(@post)
end
end
你知道为什么会这样吗?db:test:prepare不推荐使用。Rails测试助手现在自动维护您的测试模式,有关详细信息,请参阅发行说明。
describe "Add Post" do
it "shows the Post" do
visit post_path(@post)
end
end