Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 11th, 2012  |  syntax: None  |  size: 0.72 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Stubbing ActiveRecord dynamic finders with RSpec
  2. class User < ActiveRecord::Base
  3.   has_many :favorites
  4. end
  5.  
  6. class Favorite < ActiveRecord::Base
  7.   belongs_to :user
  8.   belongs_to :place
  9. end
  10.  
  11. class Place < ActiveRecord::Base
  12.   has_many :favorites, :as => :favorable
  13. end
  14.        
  15. @favorite = @current_user.favorites.find_by_place_id(@place.id)
  16.        
  17. it "should be success" do
  18.   user = double("User")
  19.   user.stub(:favorites)
  20.   get :show, :id => "1081651"
  21.   response.should be_success
  22. end
  23.        
  24. undefined method `find_by_place_id' for nil:NilClass
  25.        
  26. user.stub(:favorites).and_return(double.as_null_object)
  27.        
  28. user.stub_chain(:favorites, :find_by_place_id)
  29.        
  30. favorite = double('Favorite')
  31. user.stub_chain(:favorites, :find_by_place_id) { favorite }