我有一个测试,其中我向拥有一个帐户的用户完成付款。该帐户包含余额,并有许多交易。
付款完成后,将创建一个交易并更新帐户余额。
使用assert_difference检查事务更改次数可以得到预期的结果。但是,使用assert_difference检查余额是否已更改。我可以看到SQL来更新我的日志中的余额,但断言认为它没有改变。
我完全糊涂了,有人可以解释一下吗?
提前致谢。
通过:
assert_difference("a_user.account.transactions.count", 1) do
# Process payment
end
这失败了,说没有变化:
assert_difference("a_user.account.balance", 250.00) do
# Process payment
end
这是断言的失败消息:
"a_user.account.balance" didn't change by 250.0.
<300.0> expected but was
<50.0>.
这是日志文件中的相关SQL:
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
答案 0 :(得分:6)
尝试重新加载记录
assert_difference("a_user.account.reload.balance", 250.00) do
# Process payment
end