Michael Hartl's ROR 3.2 Tutorial - Chapter 9.4.2- test fail with error "NameError: uninitialized constant Admin"

StackOverflow https://stackoverflow.com/questions/13471502

Question

Following the MH tutorial (2nd edition) all went fine (dev and tests with Rspec and Spork) until I run Rspec at the end of chapter 9 where the test fails with that error: "NameError: uninitialized constant Admin" the log is

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

My user_pages_spec.rb around line 35 is

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

I searched around but did not find any relevant answer... I don't understand why this error appears and how to solve it... Any help would be appreciated, thanks in advance.

Cheers

EDIT (after the Dipak suggest)

For convinience I disabled Spork (in case it was the culprit) and just run RSpec Now the error message is different :

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

As you can see it seems to try to reach "{:href=>"/users.57"}" ... I suppose it should be "{:href=>"/users/57"}" instead, what I don't understand is why the

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

seems to produce {:href=>"/users.57"}...

Did I miss something essential ?

EDIT 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
Was it helpful?

Solution

Change your factories.rb like this:

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

And create admin user like this:

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

I found two error syntax in your 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 be:

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

Update

This code in _user.html.erb will check if users is not admin, they will have a delete link:

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

OTHER TIPS

try this

describe "as an admin user" do
  let(:admin) { FactoryGirl.create(:user, admin: true) }
  ...
...
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top