class Foo
def fizz(user)
puts user
end
end
let(:user){create(:user)}
it do
expect(Foo).to receive(:fizz).with(user)
end
I run this test in RSpec of Rails app. Test fails with below result
Foo received :fizz with unexpected arguments
-[#<User id: 1, .. , created_at: "2024-09-18 10:39:41.403908004 +0900", updated_at: "2024-09-18 10:39:41.579679422 +0900"]
+ [#<User id: 1, .. , created_at: "2024-09-18 10:39:41.403909000 +0900", updated_at: "2024-09-18 10:39:41.579679422 +0900"]
Reload Object
Considering the possibility that the error was caused by slight differences in the timing of when the data was saved in the database, such as created_at
and updated_at
, I used user.reload
to synchronize the database values with the values in memory, but this is solve my problem.
let(:user){create(:user)}
it do
user.reload
expect(Foo).to receive(:fizz).with(user)
end
Foo received :fizz with unexpected arguments
expected: (#<User id:1, .., created_at: "2024-09-18 10:39:41.403909000 +0900", updated_at: "2024-09-18 10:39:41.579679000 +0900")
got: (#<User id:1, .., created_at: "2024-09-18 10:39:41.403909000 +0900", updated_at: "2024-09-18 10:39:41.579679999 +0900")
6