【问题标题】:how to write rspec test for scope in model如何为模型中的范围编写 rspec 测试
【发布时间】:2017-10-27 06:06:53
【问题描述】:

我在模型中有以下范围

class ProductDetail < ApplicationRecord
  scope :currently_active, lambda {
    where('? BETWEEN start_time AND end_time', Time.zone.now)
  }
end

我为它创建了工厂,但试图弄清楚如何为它编写 rspec。

我试图做类似下面的事情

  subject { create(:product_detail) }

  describe '#currently_active' do
    context '#test' do
      it 'test' do
      product = build(:product_detail, start_time: Time.now, end_time: Time.now + 30.days)

      end
    end
  end

【问题讨论】:

  • 你不应该测试它,因为它是 Rails 的核心功能。专注于业务逻辑。
  • 我为控制器添加了 rspec 测试用例。我还需要为范围添加测试用例。

标签: ruby ruby-on-rails-4 rspec


【解决方案1】:

你可以通过做一个集合来测试这种东西,然后做

expect(Mymodel.scope).to match_array(expected_collection)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-13
    • 1970-01-01
    相关资源
    最近更新 更多