api_on_rails
api_on_rails copied to clipboard
Chapter 6 on Test sort products
test 'should sort product by most recent' do # we will touch some products to update them products(:two).touch products(:one)
I implemented the test code and code on model but test fails becuase doesn't return products in expected order, also anyone can explain me what does those two last lines? I understand that the first line updates the datetime on "updated_at" atrribute, but second line, what does?
The seconds line is a mistakes and does nothing (corrected on aa1d8464bd491f489dd7df6021d891bbf7d487f1)
About the test who fail are you sure that you have implementing recent scope ?
Ok mistake resolved :laughing: Yes I have implemented the scope, this is my code on Product model:
scope :recent, lambda {
order(:updated_at)
}
About test this fails in expected order, the error that returns the test command is:
Failure: ProductTest#test_should_sort_product_by_most_recent [/app/test/models/product_test.rb:23]: --- expected +++ actual @@ -1 +1 @@ -[#<Product id: 963905356, title: "Cheap TV", price: 0.9999e2, published: false, user_id: 298486374, created_at: "2020-01-09 18:16:17", updated_at: "2020-01-09 18:16:17">, #<Product id: 980190962, title: "TV Plosmo Philopps", price: 0.999999e4, published: false, user_id: 980190962, created_at: "2020-01-09 18:16:17", updated_at: "2020-01-09 18:16:17">, #<Product id: 298486374, title: "Azos Zeenbok", price: 0.49999e3, published: false, user_id: 298486374, created_at: "2020-01-09 18:16:17", updated_at: "2020-01-09 18:16:18">] +[#<Product id: 980190962, title: "TV Plosmo Philopps", price: 0.999999e4, published: false, user_id: 980190962, created_at: "2020-01-09 18:16:17", updated_at: "2020-01-09 18:16:17">, #<Product id: 963905356, title: "Cheap TV", price: 0.9999e2, published: false, user_id: 298486374, created_at: "2020-01-09 18:16:17", updated_at: "2020-01-09 18:16:17">, #<Product id: 298486374, title: "Azos Zeenbok", price: 0.49999e3, published: false, user_id: 298486374, created_at: "2020-01-09 18:16:17", updated_at: "2020-01-09 18:16:18">]
Thinking a bit, the default order is :asc so the result of :recent scope is [:one, :two, :another_tv] if we don't touch any, but when we touch :two this comes to final so the result will doing [:one, :another_tv, :two]. But in book expected is:
assert_equal [products(:another_tv), products(:one), products(:two)], Product.recent.to_a
I'm right or maybe I losing something?