2012-08-10 57 views
1

我在幾個地方看到過這個問題,但沒有一個解決方案似乎可行。當使用rspec,devise,guard和spork時,無法在工廠女孩中創建用戶工廠

我有一個Rails 3.1應用程序,最新版本的警衛,spork,工廠女孩,rspec和設計。

每當我嘗試創建一個用戶工廠(用戶模型是有一項模型),然後我得到這個錯誤:

Could not find a valid mapping for #<User...model attributes...> 

我不知道是什麼問題。

我跑了rake db:test:prepare。我跟着這個計算器問題的說明:"Could not find a valid mapping for #<User ...>" only on second and successive tests

另外,我試圖從谷歌集團在這個答案的解決方案:

https://groups.google.com/forum/?fromgroups#!topic/plataformatec-devise/StpbEsDCec0[1-25]

而且,這裏的所有相關代碼:

Guardfile

# A sample Guardfile 
# More info at https://github.com/guard/guard#readme 
require 'capybara/rspec' 
guard 'spork', :cucumber_env => { 'RAILS_ENV' => 'test' }, :rspec_env => { 'RAILS_ENV' => '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 => 2, :cli => '--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 
</code> 

This is in my spec/factories.rb

FactoryGirl.define do 
load "#{Rails.root}/app/models/user.rb" 
factory :user, class: User do |user| 
    email '[email protected]' 
    password '12345678' 
    password_confirmation '12345678' 
    companyid 'example_company' 
    end 
end 

This is my spec/controllers/api_controller_spec.rb

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 => 'example_company'}) 
    response.body.should include('true') 
    end 
end 

And I have this at the end of my config/application.rb

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 

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

Feel free to comment and ask any questions.

EDIT: code looked funny in places, so I edited it for clarity

UPDATE:

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.

This is my new setup (I reverted to a previous git commit and I no longer have guard or spork).

My factories.rb is exactly the same as Michael Durant's except I have an extra line:

companyid 'example' 

That's just a requirement for my app.

My spec_helper.rb requires rubygems and capybara/rspec and that's it.

And this is my spec/models/user_spec.rb

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 

Also, this is interesting: When I hit that debugger statement and type in

eval User 

It shows the mapping of a valid User.

UPDATE:

So, it's not factory girl that's the problem. It's devise.

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.

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 = User.new 
    user.email = '[email protected]' 
    user.password = '12345678' 
    user.password_confirmation = '12345678' 
    user.company_id = 'company' 
    user.save 
    post(:get_company_id, {:company_id => 'example_company'}) 
    response.body.should include('true') 
    end 
end 

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.

回答

2

I would recommend you simplify things to find the issue. Currently I feel you have too much going on/too many variable factors.

I would recommend the following:

1 Make a new branch. I assume you are using git, if not use it (git init) and make a fork.

2 Remove all the spork and guard stuff. They are helpful in speeding up your tests and running tests in a CI (Continuous Integration), but they are certainly not 'needed' and removing them will help uncover what the real problems are.

3 Set up your user factory correctly. We use this:

FactoryGirl.define do 
    sequence :email do |n| 
    "email#{n}@factory.com" 
    end 

    factory :user do 
    email 
    first_name   { 'First' } 
    last_name    { 'Last' } 
    password    { "password" } 
    password_confirmation { "password" } 
    association   :area 
    role     { 'super_user' } 
    end 

end 

4 Set up your spec_help correctly. We use these requires in our spec_helper.rb:

require 'rubygems' 
require 'capybara/rspec' 

5 Try to get one user test to pass using spec/models/user_spec.rb, something like:

require 'spec_helper' 
describe 'User associations' do 
subject { User.new } 
it { should validate_presence_of :area } 
... 
+0

檢查我的更新,看看我的嘗試使用您的建議來解決問題。簡化一切是個好主意,因爲我現在很確定這是一個涉及工廠女孩和設計的問題,但我仍然不確定該從哪裏出發。 – NielMalhotra 2012-08-10 17:57:17

1

So, the answer had nothing to do with guard, spork, rspec, or factory_girl.

The problem was that I had my devise_for :users路由註釋掉,因爲我一直在對我的rails應用進行大規模的修改。

它總是愚蠢的簡單>。 <