
Untitled
By: a guest on
May 11th, 2012 | syntax:
None | size: 0.72 KB | hits: 19 | expires: Never
Stubbing ActiveRecord dynamic finders with RSpec
class User < ActiveRecord::Base
has_many :favorites
end
class Favorite < ActiveRecord::Base
belongs_to :user
belongs_to :place
end
class Place < ActiveRecord::Base
has_many :favorites, :as => :favorable
end
@favorite = @current_user.favorites.find_by_place_id(@place.id)
it "should be success" do
user = double("User")
user.stub(:favorites)
get :show, :id => "1081651"
response.should be_success
end
undefined method `find_by_place_id' for nil:NilClass
user.stub(:favorites).and_return(double.as_null_object)
user.stub_chain(:favorites, :find_by_place_id)
favorite = double('Favorite')
user.stub_chain(:favorites, :find_by_place_id) { favorite }