Search code examples
ruby-on-railsrspecrspec-rails

rSpec - how to test select inside a model method


I have a model with a method that returns just the first name of a user and a qty.

class User < ActiveRecord::Base
  def self.list
    select("firstname, qty").order("qty desc").all
  end
end

how would I test the return value with rSpec?

User.list.should == [?????]

The method returns an array of User objects, but with only two attributes. This is where I'm stuck.


Solution

  • Since .list returns incomplete Users, your best bet may be to pull out their attributes as a Hash:

    User.list.map { |u| u.attributes }.
        should == [{ :firstname => "John", :qty => 10 }, { ... }]