Welcome to Working With Rails

 

Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
factorygirl trouble in cucumber
7 Posts
factorygirl trouble in cucumber

I'm having a hard time getting my head around this problem, so thought I'd ask if anyone else has encountered a similar issue. I've got a model with a :published boolean that hasnt been set to attr_accessible. For test purposes, I'd need to be able to set the value in my factory, and in my model spec this seems to work fine by just overriding the value with Factory(:post, :published => true). this same syntax doesnt set the value of the boolean in my cucumber step. I've traced the problem to a before_filter in my model that I use to set the boolean value when a new record is created, but it baffles me why factorygirl can skip over this in the model specs, yet not in cucumber steps.

Person
sgr
Sign In To Rate Post

I'm not terribly familiar with FactoryGirl, but I looked through the code quickly and it appears that it assigns overrides passed to Factory() via explicit setter calls rather than bulk assignment. So this:

post = Factory(:post, :published => true)

is equivalent to

post = Post.new(Factory.attributes_for(:post))
post.published = true
post.save!

If your Cucumber tests are generating Post objects via a more standard approach, most likely in your post_controller#create action:

def create
  @post = Post.new(params[:post])
  if @post.save
    ...

then it wouldn't be able to circumvent the bulk assignment protection on the published attribute.

If only that were the case, but I'm actually using the Factory in the feature in a background step to set up some records for the scenario. Heres what my background step looks like for the cucumber test in question.

Given /the following posts$/ do |table| table.hashes.each do |hash| Factory(:post, hash) end end

Which is why its so confusing, when it works in the model spec but not inside cucumber. Can this be related to where inside the rails stack cucumber tests run, or more precisely where I'm calling the Factory?

Person
sgr
Sign In To Rate Post

Are you sure that you have :published => true in you hash? I mean it's probably :published => 'true' (a string, not a booleam) since it comes from a cucumber feature definition. That may be the reason it doesn't work as expected.

I wasnt exactly sure about the type in the hash, but after looking at my feature file I figure its either of the correct type, or factorygirl simply doesnt care, because in the same table that I assign the published boolean values, I have another column called "commentable", also a boolean, and the factory sets those correctly. the only difference between the two is that commentable is attr_accessible in the model. yet both can be set with the factory when I call it in my model specs.

Person
sgr
Sign In To Rate Post

I would still try to debug what is being sent to factory girl in your step. You could try ruby debugger http://technicalpickles.com/posts/debugging-cucumber

I'll have to give that a try, just for the sake of exploration :) In the mean time, this ugly fix seems to do the trick, my step is now passing

Given /the following posts$/ do |table| table.hashes.each do |hash|

 f = Factory(:post, hash)
 f.update_attribute :published, hash[:published]

end end

Person
sgr
Sign In To Rate Post
7 Posts
Login to add your message