> ## Content Index
> Fetch the complete content index at: https://til.codes/llms.txt
> Use this file to discover other available public pages before exploring further.

# Fixing Rspec tests involving timestamps on CircleCI.
- URL: https://til.codes/fixing-rspec-tests-with-timestamps-in-circle-ci/
- Published: 2018-10-07T15:08:46.000Z
- Updated: 2018-10-07T15:09:22.000Z
- Description: Use TimeCop gem or rspec be_within built in matcher to fix specs involving timestamps in CircleCI
- Author: Manu Ajith
- Tags: rspec, circleci, ruby, #Import 2024-07-13 21:01

In one of my projects I had the following test case which depended on the timestamp of the created record.

Pretty simple thing at first sight, just making sure that we set the `confirmed_at` value when a user confirm his/her account.

```
it "updates confirmed_at and removes confirmation token" do
  confirm_account

  expect(user.confirmation_token).to be_nil
  expect(user.confirmed_at).to eq(Time.zone.now)
end
```

Now I bumped into the classic problem - this works on my machine but fails in the CircleCI.

So as usual, google fu to the rescue.

Courtesy to this [stackoverflow post](https://stackoverflow.com/questions/30139038/circleci-error-with-a-spec-involving-timestamps?ref=til.codes), I figured out this:

> `Time.now` actually has different precision on OS X and Linux machines. I would suppose that you will get the exact same result on other Linux hosts, but all OS X hosts will give you the result without rounding.

## Solutions.

1. Use [Timecop](https://github.com/travisjeffery/timecop?ref=til.codes) gem.

```
it "updates confirmed_at and removes confirmation token" do
  Timecop.freeze(Time.zone.now) do
    confirm_account

    expect(user.confirmation_token).to be_nil
    expect(user.confirmed_at).to eq(Time.zone.now)
  end
end
```

OR

2\. Rspec `be_within` matcher

```
it "updates confirmed_at and removes confirmation token" do
  confirm_account

  expect(user.confirmation_token).to be_nil
  expect(user.confirmed_at).to be_within(1).of(Time.now)
end
```

References:

[https://stackoverflow.com/questions/30139038/circleci-error-with-a-spec-involving-timestamps](https://stackoverflow.com/questions/30139038/circleci-error-with-a-spec-involving-timestamps?ref=til.codes)

[https://relishapp.com/rspec/rspec-expectations/v/3-8/docs/built-in-matchers/be-within-matcher](https://relishapp.com/rspec/rspec-expectations/v/3-8/docs/built-in-matchers/be-within-matcher?ref=til.codes)

[https://blog.eq8.eu/article/rspec-be-within-matcher.html](https://blog.eq8.eu/article/rspec-be-within-matcher.html?ref=til.codes)