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:工厂.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) }

我在您的 中发现了两个错误语法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 回答