Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Recipe model (./app/models/recipe.rb)
- class Recipe < ActiveRecord::Base
- attr_accessor :ingredients_text
- has_many :ingredients
- after_save :parse_ingredients
- protected
- def parse_ingredients
- ingredients = @ingredients_text.split("\n").map do |ingredient|
- Ingredient.find_or_create_by_name_and_recipe_id(ingredient.strip, id)
- end
- @ingredients_text = nil
- end
- end
- ## Recipe Spec (./spec/models/recipe_spec.rb)
- require File.dirname(__FILE__) + "/../spec_helper"
- context "A new Recipe" do
- setup do
- @recipe = Recipe.new do |r|
- r.name = "Testing recipe"
- r.time = "5 minutes"
- r.appliance = "any"
- r.instructions = "Mix all and serve"
- r.ingredients_text = "1 tomato
- 2 bananas
- 3 eggs"
- end
- end
- specify "should have no ingredients before saving" do
- @recipe.ingredients.should_be_empty
- end
- specify "should have ingredients after saving" do
- @recipe.save
- @recipe.ingredients.should_not_be_empty
- end
- specify "should not have the ingredients in text form after saving" do
- @recipe.save
- @recipe.ingredients_text.should_be_nil
- end
- end
- context "A saved recipe" do
- fixtures :recipes, :ingredients
- specify "should load the recipe text on demand" do
- end
- end
- ## `rake spec:models` output [plaintext]
- (in /var/www/recetarium)
- Ferret is not available on this system
- /usr/bin/ruby18 -I"/usr/lib/ruby/gems/1.8/gems/rspec-0.7.3/lib" "/usr/lib/ruby/gems/1.8/gems/rspec-0.7.3/bin/spec" "spec/models/recipe_spec.rb"
- Ferret is not available on this system
- ...F
- 1)
- TypeError in 'A saved recipe should load the recipe text on demand'
- can't convert nil into String
- Finished in 0.140132 seconds
- 4 specifications, 1 failure
- rake aborted!
- Command failed with status (1): [/usr/bin/ruby18 -I"/usr/lib/ruby/gems/1.8/...]
- (See full trace by running task with --trace)
- shell returned 1
Add Comment
Please, Sign In to add comment