require 'helper' describe 'Enhanced Finder Methods' do after do User.destroy_all end describe '.all' do it 'should return all records when no params' do User.all.to_sql.must_be_like %{ SELECT "users".* FROM "users" } end it 'extracts the non-where scopes and applies' do test_sql = User.all(:limit => 1, :offset => 2, :order => :name, :age => 10).to_sql expected_sql = User.where(:age => 10).limit(1).offset(2).order(:name).to_sql test_sql.must_be_like expected_sql end end describe '.first' do it 'calls #first on .all' do expected = User.create(:age => 10) User.create(:age => 11) User.first(:age => 10).must_equal expected end end describe '.count' do it 'calls #count on .all' do User.create(:age => 10) User.create(:age => 11) User.count(:age => 10).must_equal 1 end end describe 'hash contains :conditions' do before do @expected = User.create(:name => 'test') User.create(:name => 'no test') end it 'falls back for .all' do User.all(:conditions => "name = 'test'").must_equal [@expected] end it 'falls back for .first' do User.first(:conditions => "name = 'test'").must_equal @expected end it 'falls back for .count' do User.count(:conditions => "name = 'test'").must_equal 1 end end end