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 object 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!