How would I rspec/test an updated_at field without using sleep() in ruby?

Posted by Kamilski81 on Stack Overflow See other posts from Stack Overflow or by Kamilski81
Published on 2012-10-15T22:50:34Z Indexed on 2012/10/15 23:01 UTC
Read the original article Hit count: 189

Filed under:
|

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

© Stack Overflow or respective owner

Related posts about ruby

Related posts about rspec