使用Rspec / Capybara时未找到的Rails路由,[Rails 3.1.1,Ruby 1.9.3]

时间:2011-11-07 23:45:28

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

您好我正在使用rspec,capybara,guard,spork with rails 3.1.1和ruby 1.9.3在OSX Lion上进行轨道项目

我有以下测试

  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 routes命令的输出是

              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有相同的错误

我希望有人可以帮我解决这个问题,谢谢!

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

2 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分:0)

我刚遇到这个问题,但是我失败的原因是我不小心把我的测试代码放到没有describe块的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