开发者

Testing models with relationships and callbacks in Rails with RSpec and Factory_Girl

I'm still a working on learning RSpec so I'm sorry if completely overlooked something...

I'm writing a test for a recipe that has many ingredients. The ingredients are actually added as a percent (with a total % column on the formulation) so I want to make sure that the total column updates after every save.

So right now my RSpec test for the recipe_ingredient model has something like this:

it "should update recipe total percent" do
  @recipe = Factory.create(:basic_recipe)

  @ingredient.attributes = @valid_attributes.except(:recipe_id)
  @ingredient.recipe_id = @recipe.id
  @ingredient.percentage = 20
  @ingredient.save!

  @recipe.total_percentage.should == 20
end

I have an after_save method that just calls a quick update on the just saved receipt ingredient. It's very straightforward:

EDIT: This update_percentage action is in the recipe model. The method I call after I save an ingredient just looks up it's recipe and then calls this method on it.

def update_percentage    
  self.update_attribute(:recipe.total_percentage, self.ingredients.calculate(:sum, :percentage))
end

Am I messing something up? Do I not have access to the parent objec开发者_如何学编程t when running tests? I've tried to run a basic method to just change the parent recipe name after save but that didn't work. I'm sure it's something in the relationship I've overlooked, but all the relationships are setup correctly.

Thanks for any help/advice!


update_attribute is for updating the attributes of the current object. That means you need to call update_attribute on the object whose attribute you want to update. In this case, you want to update the recipe, not the ingredient. So you have to call recipe.update_attribute(:total_percentage, ...).

Also, ingredients belong to recipes, not other ingredients. So instead of self.ingredients.sum(:percentage) you should really be calling recipe.ingredients.sum(:percentage).

Also, you'll need to reload @recipe before testing it's total_percentage. Even though it refers to the same database record as @ingredient.recipe, it's not pointing to the same Ruby object in memory, so updates to one won't appear in the other. Reload @recipe to fetch the latest values from the database after saving the @ingredient.


Incidentally, you can build your Ingredient in a clearer manner, since you're using factory_girl already:

@ingredient = Factory(:ingredient, :recipe => @recipe, :percentage => 20)

This will build and save an Ingredient.


Hey, or you put a @recipe.reload before check for the total_percentage on recipe, or use expect.

 it "should update recipe total percent" do
  @recipe = Factory.create(:basic_recipe)
  expect {
   @ingredient.attributes = @valid_attributes.except(:recipe_id)
   @ingredient.recipe_id = @recipe.id
   @ingredient.percentage = 20
   @ingredient.save!
  }.to change(@recipe,:total_percentage).to(20)
end

I'd recommend to take a look on this presentation. Many tips about new and cool stuff on rspec. http://www.slideshare.net/gsterndale/straight-up-rspec

expect is an alias to lambda{}.should and you can read more about it here: rspec.rubyforge.org/rspec/1.3.0/classes/Spec/Matchers.html#M000168

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜