api_on_rails icon indicating copy to clipboard operation
api_on_rails copied to clipboard

Chapter 6 Listing 88/89 tests ordering without being ordered

Open notapatch opened this issue 2 years ago • 0 comments

I'm going through the book again... :-}

In listing 88 you're testing for a sort order but in listing 89 it's a WHERE clause without any ordering. SQL makes no guarantees in this situation, if it passes that's luck. You need some ordering or remove the test.

Listing 88. test/models/product_test.rb

class ProductTest < ActiveSupport::TestCase
  # ...
  test 'should filter products by name and sort them' do
    assert_equal [products(:another_tv), products(:one)], Product.filter_by_title('tv').sort
  end
end

Listing 89. app/models/product.rb

class Product < ApplicationRecord
  # ...
  scope :filter_by_title, lambda { |keyword|
    where('lower(title) LIKE ?', "%#{keyword.downcase}%")
  }
end

notapatch avatar Feb 04 '23 14:02 notapatch