← Back to Upcase

Bizarre Rspec Error -- Expected and Got match but create an error...why?


(rjewell2200) #1

I have a very bizarre message expectation in Rspec, and I was hoping that someone could help me understand it on this forum.

My, expected, and, got, are the same so I do not understand why I am seeing an error if what I got and what I expected match. Here’s the message:

1) CheckIn::Create#create with valid check in information  when the the user has an existing check_in updates the check_in
 Failure/Error: check_in_create.create
   #<CheckIn:0x007f8cc1b7a610> received :updated_at= with unexpected arguments
     expected: (2014-10-25 13:27:27 -0400)
          got: (2014-10-25 13:27:27 -0400)
 # ./app/services/check_in/create.rb:23:in `create'
 # ./spec/services/check_in/create_spec.rb:58:in `block (5 levels) in <top (required)>'

(Lourens Van Der Jagt) #2

First thing to do is verify that both instances have the same class. For example is one a String, while the other is a coerced DateTime or some such similar?


(Geoff Harcourt) #3

I would try @supaspoida’s suggestion first, as a class mismatch is possible (although I think Time and DateTime do work with an equality check).

The next thing I would check on is whether or not there’s an issue with milliseconds being off, but the milliseconds aren’t being shown as part of the output here. Issues like this are a major reason why it’s good to test things like timestamp updates with Timecop and the Timecop.freeze method so that the time isn’t moving around during your test. You can just use Time.now to check the time and the updated at time while time is frozen.


(rjewell2200) #4

@geoffharcourt and @supaspoida Thank you both. I used time cop and got it to pass. Here’s the Rspec that got it to work:

Timecop.freeze(Time.now) do
        expect(existing_user_checkin).to receive(:updated_at=).with(Time.now)
        expect(existing_user_checkin).to receive(:save)

        check_in_create_service = CheckIn::Create.new(user_mock, parameters)
        check_in_create_service.create
      end