我的规格测试出错了,我不知道如何修复它:
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
的routes.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中进行授权。
我的佣金路线输出:
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
答案 0 :(得分:0)
如果您的RSpec配置中没有以下部分,请添加它并查看是否存在问题。
config.before(:each, js: true) do
DatabaseCleaner.strategy = :truncation
end