Skip to content
This repository has been archived by the owner on Dec 22, 2017. It is now read-only.

Commit

Permalink
remove Ping#current_time and revert back o Time.now.
Browse files Browse the repository at this point in the history
cover method revert by adding time cop.
  • Loading branch information
westonplatter committed Dec 4, 2013
1 parent 1eb2f8b commit d34afd8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 12 deletions.
6 changes: 1 addition & 5 deletions app/models/ping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,11 @@ def unknown?
end

def seconds_ago
current_time.to_i - last_seen.to_i
Time.now.to_i - last_seen.to_i
end

def state
return "unknown" if unknown?
status
end

def current_time
Time.now
end
end
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'timecop'

class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
Expand Down
16 changes: 9 additions & 7 deletions test/unit/ping_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,16 @@ class PingTest < ActiveSupport::TestCase
end

test "seconds_ago" do
present = Time.now
time_change = 100
past = present - time_change

time = Time.now
time_change = 10
ping = Ping.new
ping.expects(:current_time).returns(present)
ping.expects(:last_seen).returns(past)

assert_equal(ping.seconds_ago, time_change, "true")
Timecop.freeze(time) do
ping.last_seen = Time.now
end

Timecop.travel(time + time_change) do
assert_equal(ping.seconds_ago, time_change, "true")
end
end
end

0 comments on commit d34afd8

Please sign in to comment.