robzolkos
robzolkos

Reputation: 2266

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.

Upvotes: 3

Views: 1364

Answers (2)

John Feminella
John Feminella

Reputation: 311536

factory_girl will 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]

Upvotes: 1

Rob Davis
Rob Davis

Reputation: 15772

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 }, { ... }]

Upvotes: 4

Related Questions