浏览 562 次
|
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
|---|---|
| 作者 | 正文 |
|
时间:2008-05-01
app/models/part.rb
class Part < ActiveRecord::Base
def after_create
raise "no way" if self.name == "test"
end
end
app/controllers/parts_controller.rb
class PartsController < ApplicationController
def test
Part.create!(:name => "test")
end
end
test/functional/parts_controller_test.rb
require File.dirname(__FILE__) + '/../test_helper'
class PartsControllerTest < ActionController::TestCase
def test_test
old_count = Part.count
post :test rescue nil
assert_equal(old_count, Part.count)
end
end
ruby test\functional\parts_controller_test.rb
结果是 <2> expected but was <3>. 日志是
Processing PartsController#test (for 0.0.0.0 at 2008-05-01 16:26:57) [POST]
Session ID:
Parameters: {"action"=>"test", "controller"=>"parts"}
[4;35;1mPart Create (0.000000)[0m [0mINSERT INTO parts ("name", "updated_at", "created_at") VALUES('test', '2008-05-01 16:26:57', '2008-05-01 16:26:57')[0m
[4;36;1mSQL (0.000000)[0m [0;1mSELECT count(*) AS count_all FROM parts [0m
看到没. 居然没有 回滚 换用集成测试 test/integration/model_hg_test.rb
require "#{File.dirname(__FILE__)}/../test_helper"
class ModelHgTest < ActionController::IntegrationTest
fixtures :parts
def test_truth
old_count = Part.count
puts Part.find_by_sql("select * from parts").collect(&:id).join(", ")
puts Part.find_by_sql("select * from parts").collect(&:name).join(", ")
post '/parts/test'
puts Part.find_by_sql("select * from parts").collect(&:id).join(", ")
puts Part.find_by_sql("select * from parts").collect(&:name).join(", ")
assert_equal(old_count, Part.count)
end
end
结果是
D:\t1\t1>ruby test\integration\model_hg_test.rb
Loaded suite test/integration/model_hg_test
Started
953125641, 996332877
MyString, MyString
953125641, 996332877, 996332878
MyString, MyString, test
F
Finished in 1.281 seconds.
1) Failure:
test_truth(ModelHgTest)
[test/integration/model_hg_test.rb:17:in `test_truth'
E:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/actionpack-2.0.2/lib/action_controller/integration.rb:547:in `
']:
<2> expected but was
<3>.
1 tests, 1 assertions, 1 failures, 0 errors
没有 回滚 狂汗!!! 声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |
|
时间:2008-05-01
如果在create后raise异常回滚,会让我无法理解
检查数据最好的时机应该是 # (1) before_validation # (2) before_validation_on_create # (-) validate # (-) validate_on_create |
|
| 返回顶楼 | |
|
时间:2008-05-02
这个需求很普遍
比如一个是零件表, 另一个是零件的创建日志表 比如 修改记录 等等.. 写到 after_create 比用事务代码看起来要好看和自然很多. 另外. 奇怪的是, 在 development 和 production 下, 都有回滚 只有 test 下没有回滚, 这个看似 Rails 测试的 BUG 我在官方论坛搜索一下, 没有搜到 |
|
| 返回顶楼 | |
|
时间:2008-05-02
open2ye 写道 这个需求很普遍
比如一个是零件表, 另一个是零件的创建日志表 比如 修改记录 等等.. 写到 after_create 比用事务代码看起来要好看和自然很多. 另外. 奇怪的是, 在 development 和 production 下, 都有回滚 只有 test 下没有回滚, 这个看似 Rails 测试的 BUG 我在官方论坛搜索一下, 没有搜到 干嘛不放在before_save呢? |
|
| 返回顶楼 | |
|
时间:2008-05-02
open2ye 写道 这个需求很普遍
比如一个是零件表, 另一个是零件的创建日志表 比如 修改记录 等等.. 写到 after_create 比用事务代码看起来要好看和自然很多. 另外. 奇怪的是, 在 development 和 production 下, 都有回滚 只有 test 下没有回滚, 这个看似 Rails 测试的 BUG 我在官方论坛搜索一下, 没有搜到 检查test环境的db以及对应的table是否和dev/production下一致.比如,store engine是否一致. 举个例子,如果dev和production用的是mysql,table是innodb,而test用了myisam,就有可能出现你这样的问题. 最好给出你的环境配置,不然大家只能猜. |
|
| 返回顶楼 | |
|
时间:2008-05-02
iamawalrus 写道 检查test环境的db以及对应的table是否和dev/production下一致.比如,store engine是否一致. 举个例子,如果dev和production用的是mysql,table是innodb,而test用了myisam,就有可能出现你这样的问题. 最好给出你的环境配置,不然大家只能猜. 默认配置 # SQLite version 3.x # gem install sqlite3-ruby (not necessary on OS X Leopard) development: adapter: sqlite3 database: db/development.sqlite3 timeout: 5000 # Warning: The database defined as 'test' will be erased and # re-generated from your development database when you run 'rake'. # Do not set this db to the same as development or production. test: adapter: sqlite3 database: db/test.sqlite3 timeout: 5000 production: adapter: sqlite3 database: db/production.sqlite3 timeout: 5000 |
|
| 返回顶楼 | |
|
时间:2008-05-02
gigix 写道 干嘛不放在before_save呢? 例如Part 在 before_save 前会有很多的验证. 都可以导致创建失败. 这个场景正好要做,创建成功后记录创建日志的问题 所以用 after_save 在我的场景内更合适. 撇开 用 after_save 还是 before_save 或是 xx_validation 等等. 为什么, Rails 的测试环境 要不回滚呢? 是BUG 还是故意为之, 待有空时看看源码才行啦 |
|
| 返回顶楼 | |
|
时间:2008-05-05
还有一个可能造成这个问题的原因是rails test的transactional fixture功能.
缺省是打开的,会造成transaction的嵌套,导致rollback延迟, 在test method中不能执行. 你可以试试在test_helper中把它关掉: self.use_transactional_fixtures = false 需要注意,改变这个选项可能会影响一些依赖这个功能的测试. |
|
| 返回顶楼 | |







