我正在尝试cumulative_cost
在我的Product model
.
#app/models/product.rb
class Product < ActiveRecord::Base
class << self
def cumulative_cost
self.sum(:cost)
end
end
end
所以我会运行类似Product.where(name:"surfboard").cumulative_cost
假设它返回两条记录,一条成本为 1000,另一条成本为 150,它将返回=> 1150
。
所以这就是我写的测试。
RSpec.describe Product, "class << self#cumulative_cost" do
it "should return the total cost of a collection of products"
products = Product.create!([{name:"surfboard", cost:1000}, {name:"surfboard", cost:150}])
expect(products.cumulative_cost).to eq(1150)
end
end
然后,当我运行测试时,它失败了。
undefined method `cumulative_cost' for #<Array:0x007fe3e31844e8>
任何帮助将不胜感激。