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")
#fizz. It cannot be the full test.create(:user)also seems incorrect, should it beUser.create? Please be sure you're posting the correct code which will reproduce your error. In both cases the created_at or updated_at is different. We need to see howuseris used during the example.expect(...).to have_received(...).with(...), 3) if you expect something - you need to call code that will call this somethingcreate(:user)is most likelyFactoryBot::Syntax::Methods#create.