【发布时间】:2020-04-15 06:42:55
【问题描述】:
我在 rails 中有一个命名范围,并且我有一个按名称命名的模型产品
class Product < ApplicationRecord
scope :old_products, -> { where("tagged_with = ?","old") }
end
是否有任何机构遇到过检查主题的过程,该主题在活动记录中使用 where 并且可以检查命名范围实际包含的 where 子句
在 rspec spec/models/product_spec.rb 中
describe Product do
describe "checking scope clauses" do
subject { Product.old_products }
its(:where_clauses) { should eq([
"tagged_with = 'old'"
]) }
end
end
end
顺便说一句,我使用 rspec-2.89 版本和 rails-5 版本,所以我们有机会检查和验证 where 子句
【问题讨论】:
-
除非您正在开发必须真正测试生成的 SQL 的代码,否则不应测试为您生成的 RAW SQL Active Record。如果您遵循测试行为,而不是实施策略,这对您来说会很容易。
标签: ruby-on-rails rspec