3
我有一項測試,在該測試中,我向具有一個帳戶的用戶完成付款。該帳戶包含餘額並有許多交易。Rails的assert_difference會忽略一些更改嗎?
付款完成後,將創建交易並更新帳戶餘額。
使用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
這是斷言失敗的messge:
"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
好像a_user.account.balance是越來越緩存第一次它的查詢。如果我在處理付款前後刪除assert_difference並輸出餘額,我會得到兩次50.0的輸出。但是,如果我在處理付款後只輸出餘額,我會得到預期的300.0。 – Peter 2011-02-01 17:36:05