我正在运行一个失败的测试,并显示以下消息:
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
有问题的'show'页面是:
<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)>'
尽管我的factories.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'
端
佣金路线的结果:
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
答案 0 :(得分:1)
当您在浏览器中手动转到该页面时,您可能会看到该页面,但在访问“新密钥”时,您应该会看到“新密钥”页面时遇到问题,因为new_key_path
没有存在。
如果key
是user
的孩子,那么它应该是new_user_key_path
,假设您的路线设置正确:
resources :users do
resources :keys
...
end
尝试使用new_user_key_path
代替:
<%= link_to "New Key", new_key_path,
class: "btn btn-large" %>
在你的节目中。
否则,如果key
不是user
的孩子,
您的工厂的key1没有注册,因为您需要为它定义一个单独的FactoryGirl工厂。
FactoryGirl.define do
factory :key do
...
end
end
它目前被视为user
的孩子。如果你正确缩进你的代码,你会发现你把它遗漏了:
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
# Missing end
# Missing FactoryGirl.define do
factory :key do
name "Test Key"
description "This is a test key"
is_public false
user
end
end
答案 1 :(得分:1)
鉴于它在重启spork之后起作用,你可能会遇到一个spork无法获得你所做的改变的情况。有关解决方法,请参阅Spork/Autotest not picking up changes automatically以及解决方法的最高投票答案。