我有一个模型,它的方法只返回用户名和数量。
class User < ActiveRecord::Base
def self.list
select("firstname, qty").order("qty desc").all
end
end
我如何用rSpec测试返回值?
User.list.should == [?????]
该方法返回一个User对象数组,但只有两个属性。
由于.list
返回的是不完整用户,您最好的选择可能是将它们的属性作为散列提取出来:
User.list.map { |u| u.attributes }.
should == [{ :firstname => "John", :qty => 10 }, { ... }]
factory_girl
将DRY this right up:
fewer_qty_user = Factory.create(:blank_user, :qty => 100, :firstname => 'Bob')
more_qty_user = Factory.create(:blank_user, :qty => 200, :firstname => 'Alice')
User.list.should == [more_qty_user, fewer_qty_user]