我運行一個測試,用下面的消息未能手動測試工作正常:測試沒有通過,但
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)>'
儘管有我的factories.rb文件:關鍵在它:
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
你會分享你的'config/routes.rb'文件和'rake routes'的結果嗎?至於'FactoryGirl'問題,您的'factories.rb'文件位於哪裏?此外,你正在運行'spork'或任何後臺測試過程? –
噢,如果不清楚,錯誤來自您在視圖中調用'new_key_path'。這是失敗的,因爲尚未爲其定義關聯的路線。 –
你的routes.rb文件是什麼樣子的? –