开发者

Why is RSpec is throwing an error despite the diff between the expected and received value being nil

开发者 https://www.devze.com 2023-03-25 15:53 出处:网络
I get the following RSpec error sporadically and am confused as to w开发者_如何学Pythonhat it is:

I get the following RSpec error sporadically and am confused as to w开发者_如何学Pythonhat it is:

Failure/Error: Question.all[index].created_at.should == sorted_publish_dates[index]
       expected: Tue, 02 Aug 2011 21:11:11 UTC +00:00
            got: Tue, 02 Aug 2011 21:11:11 UTC +00:00 (using ==)
       Diff:
     # ./spec/models/question_spec.rb:23:in `block (3 levels) in <top (required)>'

The expected and received values appear to be exactly the same so I'm confused as to why RSpec thinks it's a problem.


You are testing against Time.now (I suspect). Even though rspec is showing the same time string it isn't including milliseconds in the output. You will need to stub Time.now when you setup the test so that you can reliably measure that your are in fact testing for the same time as the object creation.

before do
  Time.stub!(:now).and_return("2011-02-08 21:11:11")
end


If you want to see what those time values actually are, convert them to floats via to_f before asking rspec to compare them.

Or, for a more readable value, call time.xmlschema(6) which will display microsecond precision.

You may want to rethink how you're generating these values for the test and in your app. In your app, are the 2 time values guaranteed to be the same, down to the microsecond? If so, does your test generate the times the same way?


The to_s for your items are the same. That does not mean that the items are the same. I bet that they are instances of different classes.

0

精彩评论

暂无评论...
验证码 换一张
取 消