我试图用RSpec测试Hour模型,即行为如同作用域的类方法'find_days_with_no_hours'。商业has_many与STI相关的时间。 find_days_with_no_hours需要通过Business对象调用,我无法弄清楚如何在RSpec测试中设置它。 我希望能够测试类似:测试has_many与RSpec的关联
bh = @business.hours.find_days_with_no_hours
bh.length.should == 2
我已经试过各种方法,如创建一个业务对象(比如说,Business.create),然后设置@ business.hours < < mock_model( BusinessHour,...,...),但这并不起作用。
这是如何正常完成的?
class Business < ActiveRecord::Base
has_many :hours, :as => :hourable
end
class Hour < ActiveRecord::Base
belongs_to :hourable, :polymorphic => true
def self.find_days_with_no_hours
where("start_time IS NULL")
end
end