1

我的规格测试有一个错误,我不知道如何解决它:

An error occurred in an after hook
  ActionView::Template::Error: No route matches {:controller=>"users", :action=>"show", :name=>#<User id: 1, email: "testspec@gmail.com", encrypted_password: "$2a$04$z8LP2/rW7vhP9lYQ2bSrCeZ5ASYG2O8XUgRwI3/1R2aJ...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 1, current_sign_in_at: "2013-05-01 17:21:03", last_sign_in_at: "2013-05-01 17:21:03", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", created_at: "2013-05-01 17:20:59", updated_at: "2013-05-01 17:21:03", name: nil, role: "user">}
  occurred at /Users/ejiqpep/.rvm/gems/ruby-1.9.3-p327/gems/actionpack-3.2.12/lib/action_dispatch/routing/route_set.rb:533:in `raise_routing_error'

我的测试是:

 before(:each) do
    user = FactoryGirl.create(:user)
    login_as(user, :scope => :user)
  end
  it "shouldn't allow user to delete last ingredient field", :js => true do
    visit new_recipe_path
    current_path.should == new_recipe_path
    click_link('Remove')
    page.should have_css('.ingredients-form')
  end

问题出在我的布局页面中。当我评论这一行时,一切正常。但是我自己可以单击该链接并仅在测试中出现错误。

= link_to current_user.email, current_user, :class => 'btn'

users_controller.rb

class UsersController < ApplicationController
  def show
    @user = User.find_by_name(params[:id])
  end
end

路线.rb

EcookRails::Application.routes.draw do
  devise_for :users
  devise_scope :user do
    get '/login' => 'devise/sessions#new'
    post '/login' => 'devise/sessions#create'
    get '/logout' => 'devise/sessions#destroy'
    get '/register' => 'devise/registrations#new'
  end
  resources :recipes
  resources :users

  match 'favorites/:recipe_id' => 'favorites#create', :as => :favorite
  root :to => 'recipes#index'

end

我认为问题可能出在我的用户登录 rspec。我使用 Devise 方法在 capybara 中进行授权。

我的 rake 路由输出:

 new_user_session GET    /users/sign_in(.:format)        devise/sessions#new
            user_session POST   /users/sign_in(.:format)        devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)       devise/sessions#destroy
           user_password POST   /users/password(.:format)       devise/passwords#create
       new_user_password GET    /users/password/new(.:format)   devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format)  devise/passwords#edit
                         PUT    /users/password(.:format)       devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)         devise/registrations#cancel
       user_registration POST   /users(.:format)                devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)        devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)           devise/registrations#edit
                         PUT    /users(.:format)                devise/registrations#update
                         DELETE /users(.:format)                devise/registrations#destroy
                   login GET    /login(.:format)                devise/sessions#new
                         POST   /login(.:format)                devise/sessions#create
                  logout GET    /logout(.:format)               devise/sessions#destroy
                register GET    /register(.:format)             devise/registrations#new
                 recipes GET    /recipes(.:format)              recipes#index
                         POST   /recipes(.:format)              recipes#create
              new_recipe GET    /recipes/new(.:format)          recipes#new
             edit_recipe GET    /recipes/:id/edit(.:format)     recipes#edit
                  recipe GET    /recipes/:id(.:format)          recipes#show
                         PUT    /recipes/:id(.:format)          recipes#update
                         DELETE /recipes/:id(.:format)          recipes#destroy
                   users GET    /users(.:format)                users#index
                         POST   /users(.:format)                users#create
                new_user GET    /users/new(.:format)            users#new
               edit_user GET    /users/:id/edit(.:format)       users#edit
                    user GET    /users/:id(.:format)            users#show
                         PUT    /users/:id(.:format)            users#update
                         DELETE /users/:id(.:format)            users#destroy
                favorite        /favorites/:recipe_id(.:format) favorites#create
                    root        /                               recipes#index
4

1 回答 1

0

如果您的 RSpec 配置中没有以下部分,请添加它并查看是否是问题所在。

config.before(:each, js: true) do
  DatabaseCleaner.strategy = :truncation
end
于 2013-05-01T18:36:04.837 回答