2
votes

When running bundle exec rspec on my sample rails app from the Michael Hartl tutorial I'm getting error `Argument Error: Factory not registered: user'

I have seen a bunch of extremely similar questions asked but I have tried the two solutions I have seen which are

a) Reboot the rails server b) Install 'factory_girl_rails"

Relevant sections of my Gemfile :

source 'https://rubygems.org'
ruby '2.0.0'
#ruby-gemset=railstutorial_rails_4_0

gem 'rails', '4.0.0'
gem 'bootstrap-sass', '2.3.2.0'
gem 'bcrypt-ruby', '3.0.1'

group :test do
  gem 'selenium-webdriver', '2.0.0'
  gem 'capybara', '2.1.0'   
  gem 'factory_girl_rails', '4.2.1'
end

Facotries.rb

#root/spec/factories.rb

FactoryGirl.define do
  factory :user do
    name     "Michael Hartl"
    email    "[email protected]"
    password "foobar"
    password_confirmation "foobar"
  end
end

user_pages_spec.rb

#root/spec/requests/user_pages_spec.rb

require 'spec_helper'

describe "User pages" do

  subject { page }

  describe "profile page" do
    let(:user) { FactoryGirl.create(:user) }
    before { visit user_path(user) }

    it { should have_content(user.name) }
    it { should have_title(user.name) }
  end

  describe "signup page" do
    before { visit signup_path }

    it { should have_content('Sign up') }
    it { should have_title('Sign up') }
  end
end

show.html.erb

#root/app/views/user/show.html.erb

<% provide(:title, @user.name) %>
<h1><%= @user.name %></h1>

Error output

Failures:

   1) User pages profile page 
      Failure/Error: let(:user) { FactoryGirl.create(:user) }
      ArgumentError:
        Factory not registered: user
      # ./spec/requests/user_pages_spec.rb:8:in `block (3 levels) in <top (required)>'
      # ./spec/requests/user_pages_spec.rb:9:in `block (3 levels) in <top (required)>'

I've never ceded to the stack overflow gods before but I'm about to toss my computer out of a window. Help is much appreciated!

1
It's weird, I copied your factory definition and gemfile, everything is ok on my environment. Are you sure the file name of /spec/factories.rb is not be misspelled? - Bigxiang
Thats was it! I have no idea how you caught that but you are a genius. I'm really new to this and I actually wasn't aware that the file name even mattered! - Andrew
you should answer this question yourself and mark it as answered so it's not in the 'unanswered questions' queue :) - dax

1 Answers

1
votes

Bigxiang's comment is the solution. I'm a beginner and actually didn't know that the names of my directories were important. A typo was causing the problem.