2012-11-20 51 views
2

繼MH教程(第二版)之後,一切正常(開發並測試Rspec和Spork),直到我運行Rspec爲第9章的地方測試失敗與錯誤的結尾:「NameError:未初始化的常量管理」日誌是Michael Hartl的ROR 3.2教程 - 第9.4.2-測試失敗,出現錯誤「NameError:未初始化的常量管理員」

NameError: uninitialized constant Admin 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.9/lib/active_support/inflector/methods.rb:230:in `block in constantize' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.9/lib/active_support/inflector/methods.rb:229:in `each' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.9/lib/active_support/inflector/methods.rb:229:in `constantize' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.9/lib/active_support/core_ext/string/inflections.rb:54:in `constantize' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/factory_girl-4.1.0/lib/factory_girl/factory.rb:26:in `build_class' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/factory_girl-4.1.0/lib/factory_girl/factory.rb:37:in `run' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/factory_girl-4.1.0/lib/factory_girl/factory_runner.rb:23:in `block in run' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.9/lib/active_support/notifications.rb:125:in `instrument' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/factory_girl-4.1.0/lib/factory_girl/factory_runner.rb:22:in `run' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/factory_girl-4.1.0/lib/factory_girl/strategy_syntax_method_registrar.rb:19:in `block in define_singular_strategy_method' 
C:/Users/asdemoulin/Documents/Work/dummy/spec/requests/user_pages_spec.rb:35:in `block (5 levels) in <top (required)>' 

我周圍線35 user_pages_spec.rb是

require 'spec_helper' 

describe "User Pages" do 
    subject { page } 

    describe "index" do 
    let(:user) { FactoryGirl.create(:user) } 

    before(:each) do 
     sign_in user 
     visit users_path 
    end 

    it { should have_selector('title', text: 'All users') } 
    it { should have_selector('h1', text: 'All users') } 

    describe "pagination" do 
     before(:all) { 30.times { FactoryGirl.create(:user) } } 
     after(:all) { User.delete_all } 

     it { should have_selector('div.pagination') } 

     it "should list each user" do 
     User.paginate(page: 1).each do |user| 
      page.should have_selector('li', text: user.name) 
     end 
     end 
    end 

    describe "delete links" do 
     it { should_not have_link('delete') } 

     describe "as an admin user" do 
     let(:admin) { FactoryGirl.create(:admin) } 
     before do 
      sign_in admin 
      visit users_path 
     end 
     it { should have_link('delete', href: users_path(User.first)) } 
     it "should be able to delete another user" do 
      expect { click_link('delete')}.to change(User, :count).by(-1) 
     end 
     it { should_not have_link('delete'), href: users_path(admin)} 
     end 
    end 
    end 
    ... 

我搜索周圍,但沒有沒有找到任何相關的答案... 我不清楚爲什麼這個錯誤出現以及如何解決它...任何幫助將不勝感激,在此先感謝。

乾杯

EDIT(後迪帕克建議)

對於舒適我禁用叉勺(如果它是罪魁禍首),只是運行RSpec的 現在的錯誤信息是不同的:

C:\RailsInstaller\Ruby1.9.3\bin\ruby.exe -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) C:\RailsInstaller\Ruby1.9.3\bin\rake spec 
Testing started at 12:54 ... 
NOTICE: CREATE TABLE créera des séquences implicites « users_id_seq » pour la colonne serial « users.id » 
NOTICE: CREATE TABLE/PRIMARY KEY créera un index implicite « users_pkey » pour la table « users » 
C:/RailsInstaller/Ruby1.9.3/bin/ruby.exe -S rspec ./spec/models/user_spec.rb ./spec/requests/authentication_pages_spec.rb ./spec/requests/static_pages_spec.rb ./spec/requests/user_pages_spec.rb 

expected link "delete" to return something 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-expectations-2.12.0/lib/rspec/expectations/fail_with.rb:33:in `fail_with' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-expectations-2.12.0/lib/rspec/expectations/handler.rb:33:in `handle_matcher' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/subject.rb:64:in `should' 
C:/Users/asdemoulin/Documents/Work/dummy/spec/requests/user_pages_spec.rb:40:in `block (5 levels) in <top (required)>' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/example.rb:114:in `instance_eval' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/example.rb:114:in `block in run' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/example.rb:254:in `with_around_each_hooks' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/example.rb:111:in `run' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/example_group.rb:388:in `block in run_examples' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/example_group.rb:384:in `map' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/example_group.rb:384:in `run_examples' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/example_group.rb:369:in `run' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/example_group.rb:370:in `block in run' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/example_group.rb:370:in `map' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/example_group.rb:370:in `run' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/example_group.rb:370:in `block in run' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/example_group.rb:370:in `map' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/example_group.rb:370:in `run' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/example_group.rb:370:in `block in run' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/example_group.rb:370:in `map' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/example_group.rb:370:in `run' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/command_line.rb:28:in `map' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/command_line.rb:28:in `block in run' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/reporter.rb:34:in `report' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/command_line.rb:25:in `run' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/runner.rb:69:in `run' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/runner.rb:8:in `block in autorun' 
WARNING: ignoring the provided expectation message argument ({:href=>"/users.57"}) since it is not a string. 

{:href=>"/users.57"} 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-expectations-2.12.0/lib/rspec/expectations/fail_with.rb:33:in `fail_with' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-expectations-2.12.0/lib/rspec/expectations/handler.rb:57:in `handle_matcher' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/subject.rb:78:in `should_not' 
C:/Users/asdemoulin/Documents/Work/dummy/spec/requests/user_pages_spec.rb:44:in `block (5 levels) in <top (required)>' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/example.rb:114:in `instance_eval' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/example.rb:114:in `block in run' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/example.rb:254:in `with_around_each_hooks' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/example.rb:111:in `run' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/example_group.rb:388:in `block in run_examples' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/example_group.rb:384:in `map' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/example_group.rb:384:in `run_examples' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/example_group.rb:369:in `run' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/example_group.rb:370:in `block in run' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/example_group.rb:370:in `map' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/example_group.rb:370:in `run' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/example_group.rb:370:in `block in run' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/example_group.rb:370:in `map' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/example_group.rb:370:in `run' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/example_group.rb:370:in `block in run' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/example_group.rb:370:in `map' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/example_group.rb:370:in `run' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/command_line.rb:28:in `map' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/command_line.rb:28:in `block in run' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/reporter.rb:34:in `report' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/command_line.rb:25:in `run' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/runner.rb:69:in `run' 
C:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.0/lib/rspec/core/runner.rb:8:in `block in autorun' 
rake aborted! 
C:/RailsInstaller/Ruby1.9.3/bin/ruby.exe -S rspec ./spec/models/user_spec.rb ./spec/requests/authentication_pages_spec.rb ./spec/requests/static_pages_spec.rb ./spec/requests/user_pages_spec.rb failed 
-e:1:in `load' 
-e:1:in `<main>' 
Tasks: TOP => spec 
(See full trace by running task with --trace) 

Process finished with exit code 1 

正如你可以看到它似乎試圖達到「{:href =>」/ users.57「}」...我想它應該是「{:href =>」/ users/57「}」相反,我不明白的是爲什麼

it { should_not have_link('delete'), href: users_path(admin)} 

似乎產生{:HREF => 「/ users.57」} ...

我做錯過什麼重要的?

編輯2: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" 
    end 

    factory :admin do 
    admin true 
    end 
end 
+0

請出示您的factories.rb – Thanh

回答

2

更改您的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 
end 

並創建管理員用戶是這樣的:

let(:admin) { FactoryGirl.create(:admin) } 

我發現了兩個錯誤語法在describe "delete link"

it { should have_link('delete', href: users_path(User.first)) } 
... 
it { should_not have_link('delete', href: users_path(admin))} 

它應該是:

it { should have_link('delete', href: user_path(User.first)) } 
... 
it { should_not have_link('delete', href: user_path(admin))} 

更新

這段代碼在_user.html.erb將檢查如果用戶是不管理員,他們將有一個刪除鏈接:

<% if current_user.admin? && !current_user?(user) %> 
    | <%= link_to "delete", user, method: :delete, data: { confirm: "You sure?" } %> 
<% end %> 
+0

感謝您的幫助;我現在有另一個錯誤與我的authentication_pages.rb失敗,在「rake中止」。我把它放在我原來的帖子中的第三次編輯。順便說一句,我還有一個問題,你在修改你提出的'code'it {should_not_hain_link('delete'),href:user_path(admin)}'code'爲什麼它不是'code'it {should have_link('delete', href:user_path(admin))}''code'? – phron

+0

忘記最後一條評論,我在authentication_pages_spec.rb中輸入了一個錯字。現在測試通過!再次感謝您的幫助... – phron

+0

好的,不客氣:)最後一行:'不應該有__link('delete'),href:user_path(admin)}',我記得這是一個練習章節,對於管理員不能刪除自己。 – Thanh

0

試試這個

describe "as an admin user" do 
    let(:admin) { FactoryGirl.create(:user, admin: true) } 
    ... 
... 
end 
+0

似乎解決問題的一部分,但測試再次失敗,請編輯在原來的職位 – phron

+0

@phron,你添加清單在您的應用程序/視圖/用戶9.45/_user.html.erb? –

+0

@phron,你看過代碼9.43嗎? –

相關問題