Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8687765
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T23:11:38+00:00 2026-06-12T23:11:38+00:00

How do i write my spec without using the sleep(1.second) method? When I remove

  • 0

How do i write my spec without using the sleep(1.second) method? When I remove the sleep then my tests break because they are returning the same time stamp?

I have the following class method:

def skip
qs = find_or_create_by(user_id: user_id)
qs.set_updated_at
qs.n_skip += 1
qs.save!
end

and following spec:

    qs = skip(user.id)
    sleep(1.second)
    qs2 = skip(user.id)
    qs.should_not be_nil
    qs2.should_not be_nil
    (qs.updated_at < qs2.updated_at).should be_true
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T23:11:39+00:00Added an answer on June 12, 2026 at 11:11 pm

    I’ve used the Timecop gem in the past for doing time based testing.

    require 'timecop'
    require 'test/unit'
    
    class MyTestCase < Test::Unit::TestCase
      def test_mortgage_due_in_30_days
        john = User.find(1)
        john.sign_mortgage!
        assert !john.mortgage_payment_due?
        Timecop.travel(Time.now + 30.days) do
          assert john.mortgage_payment_due?
        end
      end
    end
    

    So your example may look like:

    qs = skip(user.id)
    
    Timecop.travel(Time.now + 1.minute) do
      qs2 = skip(user.id)
    end
    
    qs.should_not be_nil
    qs2.should_not be_nil
    (qs.updated_at < qs2.updated_at).should be_true
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I;m trying to write a spec that tests a controller that is namespaced, let's
Is there a better way to write this spec? This works but I don't
I a newbie trying to write a [Ruby ON Rails] spec to make sure
I'm try to write a spec for a named scope which is date dependent.
I'm about to write a spec for a Flash developer to build a player
I try to use mock to write some unit-tests in python. For example I
I'm trying to write a request spec for a web page. This page works
What's the best way to write Resque-related specs in RSpec without stubbing the former
I'm trying to write spec for inherited_resources controller. I decided to mock all integration
I'm a bit stumped on how (and where) to write some rspec tests for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.