2012-08-10 2 views
1

이 문제는 여러 곳에서 보았지만 해결책이없는 것 같습니다.rspec, devise, guard 및 spork를 사용할 때 공장 출생의 소녀에게 사용자 팩터 리를 만들 수 없습니다.

나는 guard, spork, factory girl, rspec 및 devise의 최신 버전이 포함 된 Rails 3.1 응용 프로그램을 보유하고 있습니다.

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

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 루트는 내 레일 앱의 대대적 인 정비 작업을 수행 한 이후로 주석 처리되었습니다.

항상 멍청하게 간단합니다.> <

관련 문제