Order through created_at in unit tests with generated data in rails

I have some code that basically displays the last variable x (the variable, but let here x is 20 here) made in this table. In one of the unit tests for him, I have this snippet:

EditedItem.push_to_queue(hiddennow)
#create some new entries and save them
20.times{ EditedItem.push_to_queue(random_item) }
Queue.get_entries.each{|entry| assert_not_equal too_far_down, entry}

May or may not be beautiful, but he receives the intention. The hiddennow object has been removed too far in the queue and should no longer return when calling get_entries.

#this works
SearchObject.find(:all, :order => "id desc")

#this does not, unless the 20.times loop has sleep(1) or something
SearchObject.find(:all, :order => "created_at desc")

This is a little simplified, but it looks like the 20.times loop adds things fast enough so that the order by created_at order does not differ. My questions: am I doing something fundamentally wrong? If not, what is the best approach to writing a test on these lines?

+3
2

DigitalRoss . created_at .

- created_at :

old = EditItem.new(:created_at => 1.second.ago)
older = EditItem.new(:created_at => 2.seconds.ago)

- stubbing Time. Rspec, , Mocha.

@seconds = Time.now.to_i
Time.stub!(:now).and_return{Time.at(@seconds += 5) }

Time.now 5 .

, , , , .

+5

, ( , Rails), Unix time POSIX time, 1970 .

, 1- .

Rails hiddennow , , , 20 .

+1

Source: https://habr.com/ru/post/1722924/


All Articles