I have a method like this:
class Company < ActiveRecord::Base
before_save :update_read_status
def update_read_status
if read_changed?
column = read ? :read_at : :unread_at
self[column] = Time.zone.now
end
end
end
And I want to test the code with this RSpec
:
context "#update_read_status" do
before{ company.update_attributes(read_at: 2.months.ago) }
context "when read become true" do
it "read_at be current time" do
expect{ company.update_attributes(read: true) }.to change{ company.read_at }.from(2.months.ago).to(Time.current)
end
end
end
I know this spec fails because the time are changing during the test, but how can I compare time with change
matcher?
I found a similar question Trouble comparing time with RSpec , but the way with to_s
method or be_within
matcher is not available option in my case.