Using factory_girl_rails with Rspec on namespaced models
I have a web service that serves Ads to several different clients. The structure of the Ad varies between clients, and therefore, I am using namespaces for my models and contro开发者_高级运维llers by the client name to differentiate between Ads. From the high level, it looks like this:
'app/models/client1/ad.rb'
class Client1::Ad < ActiveRecord::Base
attr_accessible :title, :description
end
'app/models/client2/ad.rb'
class Client2::Ad < ActiveRecord::Base
attr_accessible :title, :description, :source
end
In reality, these models are more complex and have associations, but that is not the point.
I am writing some unit tests using rspec-rails 2.4.0 and factory_girl_rails 1.0.1, and all of my factories work great. However, I am not able to define factories for the namespaced models. I've tried something like:Factory.define :client1_ad, :class => Client1::Ad do |ad|
ad.title "software tester"
ad.description "Immediate opening"
end
and
Factory.define :client2_ad, :class => Client2::Ad do |ad|
ad.title "software tester"
ad.description "Immediate opening"
ad.source "feed"
end
It didn't do the job. I looked around, but every single example that I saw was using non-namespaced models. Anyone have any ideas? Any input is greatly appreciated.
I have a minimal working example here, maybe you could use it to pinpoint where your problem is. The comment you left on dmarkow's answer suggests to me that you have an error someplace else.
app/models/bar/foo.rb
class Bar::Foo < ActiveRecord::Base
end
*db/migrate/20110614204536_foo.rb*
class Foo < ActiveRecord::Migration
def self.up
create_table :foos do |t|
t.string :name
end
end
def self.down
drop_table :foos
end
end
spec/factories.rb
Factory.define :foo, :class => Bar::Foo do |f|
f.name 'Foooo'
end
*spec/models/foo_spec.rb*
require 'spec_helper'
describe Bar::Foo do
it 'does foo' do
foo = Factory(:foo)
foo.name.should == 'Foooo'
end
end
Running the test:
$ rake db:migrate
$ rake db:test:prepare
$ rspec spec/models/foo_spec.rb
.
Finished in 0.00977 seconds
1 example, 0 failures
Hope it helps.
I think maybe FactoryGirl changes since this answer was posted. I did to make it work
Factory.define do
factory :foo, :class => Bar::Foo do |f|
f.name 'Foooo'
end
end
With the current latest version of FactoryGirl
(4.5.0), this is the syntax:
FactoryGirl.define do
factory :client1_ad, class: Client1::Ad do |f|
f.title "software tester"
f.description "Immediate opening"
end
end
Notice that client1_ad
can be whatever name you want coz we already force identifying its class name.
I found this question looking into a related issue with FactoryGirl and after a little reading of the source I figured out I could solve my problem by renaming my factories.
When I had model classes that were namespaced inside modules, eg: Admin::User
I should have been defining my factories like this:
factory :'admin/user', class: Admin::User do
#...
end
Rather than:
factory :admin_user, class: Admin::User do
#...
end
Maybe this little tidbit of info might help someone someday. The specifics of my issue was that I was trying to use build(described_class)
to build instances from factories in my RSpec
specs and this works just fine with un-namespaced classes but not with classes inside modules. The reason is that internally when looking up factories FactoryGirl will use ActiveSupport
's underscore
helper to normalise the factory name.
Have you tried passing the actual class, rather than a string with the class name:
Factory.define :client1_ad, :class => Client1::Ad do |ad|
精彩评论