Simulate the current date during testing

I am writing tests, and I am wondering if there is a way to change the current date during my test?

The fact is that I am testing the statistical functionality of the application, which is associated with the tracking model, which creates an instance every time an action is called (some actions are only tracked using the call "for interrogation only")

Therefore, I need to call these actions in different controllers at different points in time to verify that my analytic component is performing the correct calculations, but I did not find a way to implement change_current_time in the following code example:

test "login count" do
  change_current_time(2.day.ago)
  get "users/login/testuser/testpassword"
  assert login_count(2.day.ago) == 1
  change_current_time(1.day.ago)
  get "users/login/testuser/testpassword"
  get "users/login/testuser1/testpassword1"
  assert login_count(1.day.ago) == 2
end
+3
source share
5

- Mock /, .

http://en.wikipedia.org/wiki/Mock_object

, , , , . , .

, , .

+1

Rails .  
https://github.com/harvesthq/time-warp

time = (2.days.ago)

pretend_now_is(time) do
  get "users/login/testuser/testpassword"
  assert login_count(Date.today) == 1 # it stubbed
end

time = (1.days.ago)

pretend_now_is(time) do
  get "users/login/testuser/testpassword"
  get "users/login/testuser1/testpassword1"
  assert login_count(Date.today) == 2
end
0

This question was asked some time ago, but now there is a solution that does not require additional stones or installation.

time = 2.days.ago
travel_to(time)

Source: http://api.rubyonrails.org/v5.1/classes/ActiveSupport/Testing/TimeHelpers.html

0
source

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


All Articles