rspec - 使用 Rspec/Capybara 时找不到 Rails 路线,[Rails 3.1.1,Ruby 1.9.3]

标签 rspec ruby-on-rails-3.1 routes capybara

您好,我正在 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>

但是我的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.
#

最佳答案

问题是我没有运行 rake db:test:prepare 命令:P

关于rspec - 使用 Rspec/Capybara 时找不到 Rails 路线,[Rails 3.1.1,Ruby 1.9.3],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8044274/

相关文章:

redirect - 语言切换器,使用symfony2.0重定向到当前页面

ruby-on-rails-3 - Rspec 头痛 : configuration advice appreciated

ruby-on-rails - 允许 eq 比较中的数组内容是 rspec 测试中的任何内容

ruby-on-rails - 从 Rails 控制台运行迁移

node.js - Swagger-node-express 用法

ember.js - "basic"是 Ember 中的保留路由名称吗?

ruby-on-rails - rails 和 RSpec : Testing CRUD actions with shared examples

ruby - 在 Ruby 1.9.3 中通过 RSpec 配置包含命名空间模块

jquery - 如何以嵌套形式使用 jQuery FIle Upload?

ruby-on-rails - 渴望加载关联但限制返回