Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 6 on Test sort products #37

Open
oscartzgz opened this issue Jan 7, 2020 · 2 comments
Open

Chapter 6 on Test sort products #37

oscartzgz opened this issue Jan 7, 2020 · 2 comments

Comments

@oscartzgz
Copy link
Contributor

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?

@madeindjs
Copy link
Owner

The seconds line is a mistakes and does nothing (corrected on aa1d846)

About the test who fail are you sure that you have implementing recent scope ?

@oscartzgz
Copy link
Contributor Author

Ok mistake resolved 😆
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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants