我正在运行一个失败的测试,并显示以下消息:
1) User pages profile page
Failure/Error: before { visit user_path(user) }
ActionView::Template::Error:
undefined local variable or method `new_key_path' for #<#<Class:0x007fa775c4bc50>:0x007fa775c48910>
# ./app/views/users/show.html.erb:21:in `_app_views_users_show_html_erb__4517315841728073114_70178605538620'
# ./spec/requests/user_pages_spec.rb:58:in `block (3 levels) in <top (required)>'
但是,当我查看页面并手动点击链接时,它可以正常工作。这是测试的代码:
describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
# let!(:key1) { FactoryGirl.create(:key) }
let!(:key1) { user.keys.build(name: "test", description: "test description", is_public: false) }
before { visit user_path(user) }
it { should have_content(user.name) }
it { should have_title(user.name) }
describe "keys" do
it { should have_content(key1.name) }
it { should have_content(key1.description) }
end
end
有问题的“显示”页面是:
<div class="row">
<div class="span12">
<% if @user.keys.any? %>
<h3>My Keys (<%= @user.keys.count %>)</h3>
<ol class="microposts">
<%= render @keys %>
</ol>
<%= will_paginate @keys %>
<% end %>
<%= link_to "New Key", new_key_path,
class: "btn btn-large" %>
知道是什么导致测试失败吗?
在一个有点不相关的注释中,您可能已经注意到我没有使用工厂来处理 :key1。这是因为我在使用它时不断收到此错误:
1) User pages profile page
Failure/Error: let!(:key1) { FactoryGirl.create(:key) }
ArgumentError:
Factory not registered: key
# ./spec/requests/user_pages_spec.rb:55:in `block (3 levels) in <top (required)>'
尽管我的 factory.rb 文件中有 :key :
FactoryGirl.define do
factory :user do
sequence(:name) { |n| "Person #{n}" }
sequence(:email) { |n| "person_#{n}@example.com"}
password "foobar"
password_confirmation "foobar"
factory :admin do
admin true
end
end
factory :key do
name "Test Key"
description "This is a test key"
is_public false
user
end
end
任何帮助将非常感激。谢谢。
编辑:添加路线
SampleApp::Application.routes.draw do
get "keys/new"
resources :users
resources :sessions, only: [:new, :create, :destroy]
resources :keys, only: [:index, :new, :destroy, :create]
root to: 'static_pages#home'
match '/signup', to: 'users#new', via: 'get'
match '/signin', to: 'sessions#new', via: 'get'
match '/signout', to: 'sessions#destroy', via: 'delete'
match '/help', to: 'static_pages#help', via: 'get'
match '/about', to: 'static_pages#about', via: 'get'
match '/contact', to: 'static_pages#contact', via: 'get'
结尾
rake 路由的结果:
Prefix Verb URI Pattern Controller#Action
keys_new GET /keys/new(.:format) keys#new
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
PATCH /users/:id(.:format) users#update
PUT /users/:id(.:format) users#update
DELETE /users/:id(.:format) users#destroy
sessions POST /sessions(.:format) sessions#create
new_session GET /sessions/new(.:format) sessions#new
session DELETE /sessions/:id(.:format) sessions#destroy
keys GET /keys(.:format) keys#index
POST /keys(.:format) keys#create
new_key GET /keys/new(.:format) keys#new
key DELETE /keys/:id(.:format) keys#destroy
root GET / static_pages#home
signup GET /signup(.:format) users#new
signin GET /signin(.:format) sessions#new
signout DELETE /signout(.:format) sessions#destroy
help GET /help(.:format) static_pages#help
about GET /about(.:format) static_pages#about
contact GET /contact(.:format) static_pages#contact