Guest User

Untitled

a guest
Feb 21st, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. ## Recipe model (./app/models/recipe.rb)
  2. class Recipe < ActiveRecord::Base
  3. attr_accessor :ingredients_text
  4. has_many :ingredients
  5.  
  6. after_save :parse_ingredients
  7.  
  8. protected
  9.  
  10. def parse_ingredients
  11. ingredients = @ingredients_text.split("\n").map do |ingredient|
  12. Ingredient.find_or_create_by_name_and_recipe_id(ingredient.strip, id)
  13. end
  14. @ingredients_text = nil
  15. end
  16. end
  17.  
  18. ## Recipe Spec (./spec/models/recipe_spec.rb)
  19. require File.dirname(__FILE__) + "/../spec_helper"
  20.  
  21. context "A new Recipe" do
  22. setup do
  23. @recipe = Recipe.new do |r|
  24. r.name = "Testing recipe"
  25. r.time = "5 minutes"
  26. r.appliance = "any"
  27. r.instructions = "Mix all and serve"
  28. r.ingredients_text = "1 tomato
  29. 2 bananas
  30. 3 eggs"
  31. end
  32. end
  33.  
  34. specify "should have no ingredients before saving" do
  35. @recipe.ingredients.should_be_empty
  36. end
  37.  
  38. specify "should have ingredients after saving" do
  39. @recipe.save
  40. @recipe.ingredients.should_not_be_empty
  41. end
  42.  
  43. specify "should not have the ingredients in text form after saving" do
  44. @recipe.save
  45. @recipe.ingredients_text.should_be_nil
  46. end
  47. end
  48.  
  49. context "A saved recipe" do
  50. fixtures :recipes, :ingredients
  51.  
  52. specify "should load the recipe text on demand" do
  53. end
  54. end
  55.  
  56. ## `rake spec:models` output [plaintext]
  57. (in /var/www/recetarium)
  58. Ferret is not available on this system
  59. /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"
  60.  
  61. Ferret is not available on this system
  62.  
  63. ...F
  64.  
  65. 1)
  66. TypeError in 'A saved recipe should load the recipe text on demand'
  67. can't convert nil into String
  68.  
  69.  
  70. Finished in 0.140132 seconds
  71.  
  72. 4 specifications, 1 failure
  73. rake aborted!
  74. Command failed with status (1): [/usr/bin/ruby18 -I"/usr/lib/ruby/gems/1.8/...]
  75.  
  76. (See full trace by running task with --trace)
  77.  
  78. shell returned 1
Add Comment
Please, Sign In to add comment