Using Rspec, factory_girl and permalink?

Hi!

I’m using Rspec and factory_girl. In my products model I have a method that creates permalinks simply by overriding to_param, like this:

def create_permalink
self.permalink = “#{self.name} #{self.brand.name}”.parameterize
end

My product factory looks like this:

Factory.define :product do |p|
p.sequence(:name) { |n| ‘Product Name #{n}’}
p.gender ‘Man’
p.sequence(:price) { |n| 123.00 + n }
p.product_url ‘http://www.example.com/prod-url
p.association :reseller
p.association :subcategory
end

Then when I try to use the factory in Rspec, like this:

Factory.create(:product)

I get the following error:

Failure/Error: Factory.create(:product)
NoMethodError: undefined method `name’ for nil:NilClass

Why is that?

Regards
Linus

def create_permalink
self.permalink = "#{self.name} #{self.brand.name}".parameterize
end

...

Failure/Error: Factory.create(:product)
NoMethodError: undefined method `name' for nil:NilClass

Why is that?

My guess would be that you're not setting self.brand. I don't see
anything in the Factory setting it.

-Dave

Of course… Thank you! Sometimes when you’ve looked for too long on the code you get blind :slight_smile:

Regards

Linus