RSpec、つまりスコープのように動作するクラス メソッド 'find_days_with_no_hours' を使用して Hour モデルをテストしようとしています。ビジネス has_many STI を介して関連付けられた時間数。find_days_with_no_hours は Business オブジェクトを介して呼び出す必要があり、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