【发布时间】:2011-12-29 08:05:22
【问题描述】:
如何设置以下模型/规格以在我的控制器规格中进行模拟。
这是模型
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
我可以用 :favorites 做什么来帮助它通过。由于使用了动态查找器,我不确定如何正确模拟它。
【问题讨论】:
标签: ruby-on-rails activerecord rspec rspec2