I have a test in which I complete a payment to a user who has one account. The account contains a balance and has many transactions.
When payment is completed, a transaction is created and the balance in the account is updated.
Using assert_difference to check the number of transaction changes gives the expected result. However, using assert_difference to verify that the balance has changed does not work. I can see that SQL is updating the balance in my log, but the statement believes that it has not changed.
I'm completely confused, can anyone explain?
Thanks in advance.
It goes through:
assert_difference("a_user.account.transactions.count", 1) do
end
This fails, saying there are no changes:
assert_difference("a_user.account.balance", 250.00) do
end
This is the error message from the statement:
"a_user.account.balance" didn't change by 250.0.
<300.0> expected but was
<50.0>.
This is the corresponding SQL from the log file:
User Load (0.4ms) SELECT * FROM `users` WHERE (`users`.`id` = 108093344)
Account Load (0.2ms) SELECT * FROM `accounts` WHERE (`accounts`.user_id = 108093344) LIMIT 1
Transaction Columns (1.5ms) SHOW FIELDS FROM `transactions`
Transaction Create (0.3ms) INSERT INTO `transactions` (`created_at`, `updated_at`, `amount`, `account_id`, `detail`) VALUES('2011-02-01 17:15:21', '2011-02-01 17:15:21', 250.0, 956948796, 'Payment completed')
Account Load (0.2ms) SELECT * FROM `accounts` WHERE (`accounts`.`id` = 956948796)
Account Update (0.2ms) UPDATE `accounts` SET `updated_at` = '2011-02-01 17:15:21', `balance` = 300.0 WHERE `id` = 956948796