2013-08-06 98 views
0

我運行一個測試,用下面的消息未能手動測試工作正常:測試沒有通過,但

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 
+1

你會分享你的'config/routes.rb'文件和'rake routes'的結果嗎?至於'FactoryGirl'問題,您的'factories.rb'文件位於哪裏?此外,你正在運行'spork'或任何後臺測試過程? –

+0

噢,如果不清楚,錯誤來自您在視圖中調用'new_key_path'。這是失敗的,因爲尚未爲其定義關聯的路線。 –

+0

你的routes.rb文件是什麼樣子的? –

回答

1

你可能看到的頁面,當你手動去它在你的瀏覽器中,但在訪問「新密鑰」時,你應該看到「新密鑰」頁面出現問題e,因爲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 
+0

謝謝@amyhua,我已將路線添加到上面的代碼中。爲了澄清,我可以直接從我的瀏覽器訪問New Key頁面,並且從用戶/演示頁面添加新的密鑰沒有任何問題。再次感謝! – jackerman09

+0

我重置我的spork服務器,測試現在通過。很奇怪,但感謝您的幫助。任何想法可能造成的? – jackerman09

相關問題