Delayed Job converting time, giving weird mocha expectation error
I'm not sure if you guys test like this, but I'm a TDD guy and keep stumbling into wierd stuff. The timestamps are converted somehow by DJ, or the time zone... I don't know. Test example follows
I'm using delayed_job 2.0.3
data = {:value => 0.856, :timestamp => Time.zone.now}
job = MyMailer.send_later :send_values, data, emails
MyMailer.expects(:send_values).with(data, emails).once
job.payload_object.perform
class MyMailer
def self.send_values(data, emails)
end
end
OK, test expectation failure
unexpected invocation: MyMailer.send_values({:value => 0.856576407208833, :timestamp => Thu Nov 11 22:01:00 UTC 2010 (1289512860.94962 secs)}, ..
unsatisfied expectations:
- expected exactly once, not yet invoked: MyMailer.send_values({:value =&g开发者_StackOverflow中文版t; 0.856576407208833, :timestamp => Thu, 11 Nov 2010 23:01:00 CET +01:00}...
with datetime it's similar, DateTime.now
instead of Time.zone.now
got :timestamp => Thu Nov 11 23:13:33 +0100 2010 (1289513613.0 secs)
expected :timestamp => 2010-11-11T23:13:33+01:00
What's happening? How can I control it (do I want to)?
I thought I had the answer to this when I first saw the question. But I don't anymore, so here is a guess:
Since Time.zone.now, DateTime.now and Time.now are slightly different from each other, and Zone is something Rails has created (I think), could it be that ruby somewhere in your testing framework misses out on the zone-thingy? Time.zone.now.to_datetime has rescued me from similar stuff before. It lets you use zone, and you get the same format as DateTime.now witch lacks DateTime.zone.now.
Try: data = {:value => 0.856, :timestamp => Time.zone.now.to_datetime}
精彩评论