2

MH チュートリアル (第 2 版) に従って、すべて問題なく (Rspec と Spork を使用した開発とテスト)、第 9 章の最後で Rspec を実行すると、そのエラーでテストが失敗します: "NameError: uninitialized constant Admin" ログは次のとおりです。

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
  ...

検索しましたが、関連する回答が見つかりませんでした...このエラーが表示される理由と解決方法がわかりません...助けていただければ幸いです。

乾杯

編集(Dipakの提案後)

便宜上、Spork を無効にし (それが原因である場合に備えて)、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: factory.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
4

2 に答える 2

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) }

あなたの中に2つのエラー構文が見つかりました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 %>
于 2012-11-20T12:19:25.863 に答える
0

これを試して

describe "as an admin user" do
  let(:admin) { FactoryGirl.create(:user, admin: true) }
  ...
...
end
于 2012-11-20T11:11:01.097 に答える