Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Add "Prefer freeze_time over travel_to with an argument of the current time" rule #318

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

Merged
bbatsov merged 1 commit into rubocop:master from ydah:add_freeze_time_rule
Jun 15, 2022

Conversation

@ydah
Copy link
Member

@ydah ydah commented Jun 14, 2022

Since there are several ways to indicate the current time, we believe that freeze_time, which can be expressed as simply as possible, should be preferred.

# bad
travel_to(Time.now)
travel_to(DateTime.now)
travel_to(Time.current)
travel_to(Time.zone.now)
travel_to(Time.now.in_time_zone)
travel_to(Time.current.to_time)
# good
freeze_time

pirj reacted with thumbs up emoji
Copy link
Member

pirj commented Jun 14, 2022

It is worth mentioning that freeze_time just delegates to travel_to with a default Time.now (https://github.com/rails/rails/blob/aa3a6c9c1b70e49bef5a2713aae96179debdff3e/activesupport/lib/active_support/testing/time_helpers.rb#L237):

 def freeze_time(&block)
 travel_to Time.now, &block
 end

@pirj pirj requested a review from a team June 14, 2022 11:03
...rrent time" rule
Since there are several ways to indicate the current time,
we believe that `freeze_time`,
which can be expressed as simply as possible, should be preferred.
```ruby
# bad
travel_to(Time.now)
travel_to(DateTime.now)
travel_to(Time.current)
travel_to(Time.zone.now)
travel_to(Time.now.in_time_zone)
travel_to(Time.current.to_time)
# good
freeze_time
```
@ydah ydah force-pushed the add_freeze_time_rule branch from e97790e to 3cb6abe Compare June 14, 2022 13:06
@ydah ydah changed the title (削除) Add "Prefer freeze_time over travel_to with argument current time" rule (削除ここまで) (追記) Add "Prefer freeze_time over travel_to with an argument of the current time" rule (追記ここまで) Jun 14, 2022
@ydah ydah requested a review from andyw8 June 14, 2022 13:23
@bbatsov bbatsov merged commit 65c8789 into rubocop:master Jun 15, 2022
Copy link
Collaborator

bbatsov commented Jun 15, 2022

Thanks!

ydah reacted with heart emoji

@ydah ydah deleted the add_freeze_time_rule branch June 15, 2022 08:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@andyw8 andyw8 andyw8 approved these changes

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /