title: Can't create a user factory in factory girl when using rspec, devise, guard, and spork
body: <p>I've seen this issue several places, but none of the solutions seem to work.</p>
  
  <p>I have a Rails 3.1 app with the latest versions of guard, spork, factory girl, rspec, and devise.</p>
  
  <p>Whenever I try to create a user factory (the user model is a devise model) then I get this error:</p>
  
  <pre><code>Could not find a valid mapping for #&lt;User...model attributes...&gt;
  </code></pre>
  
  <p>I'm not sure what the problem is.</p>
  
  <p>I ran <code>rake db:test:prepare</code>. I followed the instructions in this stackoverflow question: <a href="https://stackoverflow.com/questions/6363471/could-not-find-a-valid-mapping-for-user-only-on-second-and-successive-t">&quot;Could not find a valid mapping for #&lt;User ...&gt;&quot; only on second and successive tests</a></p>
  
  <p>ALso, I attempted the solution in this answer from google groups:</p>
  
  <p><a href="https://groups.google.com/forum/?fromgroups#!topic/plataformatec-devise/StpbEsDCec0[1-25]" rel="nofollow noreferrer">https://groups.google.com/forum/?fromgroups#!topic/plataformatec-devise/StpbEsDCec0[1-25]</a></p>
  
  <p>And, here's all the relevant code:</p>
  
  <p>Guardfile
  <code></p>
  
  <pre><code>    # A sample Guardfile
  # More info at https://github.com/guard/guard#readme
  require 'capybara/rspec'
  guard 'spork', :cucumber_env =&gt; { 'RAILS_ENV' =&gt; 'test' }, :rspec_env =&gt; { 'RAILS_ENV' =&gt; 'test' } do
    watch('config/application.rb')
    watch('config/environment.rb')
    watch('config/environments/test.rb')
    watch(%r{^config/initializers/.+\.rb$})
    watch('Gemfile')
    watch('Gemfile.lock')
    watch('spec/spec_helper.rb') { :rspec }
    watch('test/test_helper.rb') { :test_unit }
    watch(%r{features/support/}) { :cucumber }
  end
  
  guard 'rspec', :version =&gt; 2, :cli =&gt; '--drb' do
    watch(%r{^spec/.+_spec\.rb$})
    watch(%r{^lib/(.+)\.rb$})     { |m| "spec/lib/#{m[1]}_spec.rb" }
    watch('spec/spec_helper.rb')  { "spec" }
  
    # Rails example
    watch(%r{^app/(.+)\.rb$})                           { |m| "spec/#{m[1]}_spec.rb" }
    watch(%r{^app/(.*)(\.erb|\.haml)$})                 { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
    watch(%r{^app/controllers/(.+)_(controller)\.rb$})  { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
    watch(%r{^spec/support/(.+)\.rb$})                  { "spec" }
    watch('config/routes.rb')                           { "spec/routing" }
    watch('app/controllers/application_controller.rb')  { "spec/controllers" }
  
    # Capybara request specs
    watch(%r{^app/views/(.+)/.*\.(erb|haml)$})          { |m| "spec/requests/#{m[1]}_spec.rb" }
  
    # Turnip features and steps
    watch(%r{^spec/acceptance/(.+)\.feature$})
    watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$})   { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
  end
  &lt;/code&gt;
  </code></pre>
  
  <p>This is in my spec/factories.rb</p>
  
  <pre><code>FactoryGirl.define do   
   load "#{Rails.root}/app/models/user.rb"  
   factory :user, class: User do |user|
      email 'owner@example.com'
       password '12345678'
      password_confirmation '12345678'
       companyid 'example_company'   
    end 
  end
  </code></pre>
  
  <p>This is my spec/controllers/api_controller_spec.rb</p>
  
  <pre><code>require 'spec_helper'
  
  describe ApiController do
    it 'verifies company_id through POST to api/company_id' do
      load "#{Rails.root}/app/models/user.rb"
      debugger
      user = FactoryGirl.create(:user)
      post(:get_company_id, {:company_id =&gt; 'example_company'})
      response.body.should include('true')
    end
  end
  </code></pre>
  
  <p>And I have this at the end of my config/application.rb</p>
  
  <pre><code>    ActionDispatch::Callbacks.after do
    # Reload the factories
    return unless (Rails.env.development? || Rails.env.test?)
  
    unless FactoryGirl.factories.blank? # first init will load factories, this should only run on subsequent reloads
      FactoryGirl.factories.clear
      FactoryGirl.find_definitions
    end
  end
  </code></pre>
  
  <p>I'm really desperate for an answer here because otherwise I won't be able to test my User model (which is the most important model I have).</p>
  
  <p>Feel free to comment and ask any questions.</p>
  
  <p>EDIT: code looked funny in places, so I edited it for clarity</p>
  
  <p>UPDATE:</p>
  
  <p>So I tried simplifying everything to get to the core of the problem, and I'm pretty sure that devise and factory girl don't "like" each other. I'm still getting the exact same error whenever I try and create a user factory.</p>
  
  <p>This is my new setup (I reverted to a previous git commit and I no longer have guard or spork).</p>
  
  <p>My factories.rb is exactly the same as Michael Durant's except I have an extra line:</p>
  
  <pre><code>companyid 'example'
  </code></pre>
  
  <p>That's just a requirement for my app.</p>
  
  <p>My spec_helper.rb requires rubygems and capybara/rspec and that's it.</p>
  
  <p>And this is my spec/models/user_spec.rb</p>
  
  <pre><code>require 'spec_helper'
    describe 'User associations' do
  
      it 'tests creation of user' do
        debugger
        user = FactoryGirl.create(:user)
        User.count.should be(1)
  
      end
    end
  </code></pre>
  
  <p>Also, this is interesting: When I hit that debugger statement and type in </p>
  
  <pre><code> eval User
  </code></pre>
  
  <p>It shows the mapping of a valid User.</p>
  
  <p>UPDATE:</p>
  
  <p>So, it's not factory girl that's the problem. It's devise.</p>
  
  <p>This is the new api_controller_spec.rb file and it comes up with the same error of not having a valid mapping of the user.</p>
  
  <p>require 'spec_helper'</p>
  
  <pre>describe ApiController do
    it 'verifies company_id through POST to api/company_id' do
      load "#{Rails.root}/app/models/user.rb"
      debugger
      user = User.new
      user.email = 'owner@example.com'
      user.password = '12345678'
      user.password_confirmation = '12345678'
      user.company_id = 'company'
      user.save
      post(:get_company_id, {:company_id =&gt; 'example_company'})
      response.body.should include('true')
    end
  end
  </code></pre>
  
  <p>THere isn't a problem with any other environment as I can create users fine through the console, while a local server is running, or when the code is pushed up to Heroku. It might be rspec or something else, but I'm just not sure at this point.</p>