How can I setup the following models/specs for mocking in my controller specs.
Here are the models
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
Then at some point a place will be checked to see if it’s currently a favorite
@favorite = @current_user.favorites.find_by_place_id(@place.id)
Now, I want to mock a user for some examples
it "should be success" do
user = double("User")
user.stub(:favorites)
get :show, :id => "1081651"
response.should be_success
end
But, I end up with
undefined method `find_by_place_id' for nil:NilClass
What can I do with :favorites to help it pass. Since the dynamic finder is used, I’m not sure how to properly mock that up.
Use null object stub, which will ignore all unexpected messages:
Another way is to use
stub_chainmethod: