rails的fixtures有一个令人讨厌的地方:
fixtures 的数据不会在测试结束后自动清除 ,这样就使得fixtures遗留的数据影响到后来的测试。
相关的争论也持续了很久 ,具体的连接请看 http://dev.rubyonrails.org/ticket/2404
里面的 Rick的patch我在rspec下用了,不见好用,我只能自己搞了一个patch,在rspec的 spec_helper.rb下引入
,解决了这个问题 。
patch主要的思路是: 每次测试setup运行前插入fixture的数据,保证这个插入 fixtures数据的事务和test运行时的事务是同一个 ,teardown结束前,清空一些类变量, 原有的teardown运行的时候会作事务回滚的动作,这样就可以保证每次测试都回滚圆来插入的 fixtures的数据。
相关的配置 :
config.use_transactional_fixtures = true
config.use_instantiated_fixtures = false
module Test #:nodoc:
module Unit #:nodoc:
class TestCase #:nodoc:
alias_method : old_setup_with_fixtures, :setup_with_fixtures unless method_defined?(: old_setup_with_fixtures)
alias_method : old_teardown_with_fixtures, :teardown_with_fixtures unless method_defined?(: old_teardown_with_fixtures)
def setup_with_fixtures
if use_transactional_fixtures?
ActiveRecord::Base.send :increment_open_transactions
ActiveRecord::Base.connection.begin_db_transaction
close_original_activerecord_transaction_methods
end
old_setup_with_fixtures
if use_transactional_fixtures?
open_original_activerecord_transaction_methods
end
end
def teardown_with_fixtures
if use_transactional_fixtures?
clear_fixtures_states_when_use_transactional_fixtures
end
old_teardown_with_fixtures
end
#prevent the next code:alias_method from trigger invoking the self.method_added introspected method
class<<TestCase
alias old_method_added method_added
def method_added(m)
#do nothing
end
end
alias_method :setup,:setup_with_fixtures
alias_method :teardown,:teardown_with_fixtures
#reopen the introspector class method:method_added
class<<TestCase
alias method_added old_method_added
end
private
def clear_fixtures_states_when_use_transactional_fixtures
@@already_loaded_fixtures.clear if @@already_loaded_fixtures
@loaded_fixtures.clear if @loaded_fixtures
end
def close_original_activerecord_transaction_methods
self.class.class_eval(%Q[
class<<ActiveRecord::Base
alias old_increment_open_transactions increment_open_transactions
def increment_open_transactions
#do nothing
end
end
])
ActiveRecord::Base.connection.class.class_eval(%Q[
alias_method : old_begin_db_transaction,:begin_db_transaction
def begin_db_transaction
#do nothing
end
])
end
def open_original_activerecord_transaction_methods
self.class.class_eval(%Q[
class<<ActiveRecord::Base
alias increment_open_transactions old_increment_open_transactions
end
])
ActiveRecord::Base.connection.class.class_eval(%Q[
alias_method :begin_db_transaction,: old_begin_db_transaction
])
end
end
end
end
分享到:
- 2007-12-08 11:17
- 浏览 1963
- 评论(3)
- 论坛回复 / 浏览 (3 / 3039)
- 查看更多
相关推荐
大家经常会在维护AD的同时有可能误删除用户,OU... QUEST Object Restore for Active Directory(这是QUEST很少的免费软件之一,相比QUEST Recovery Manager for Active Directory功能弱很多,但是我们还是可以使用到)
A dump/restore is not required for those running 14.X. However, note that installations using physical replication should update standby servers before the primary server, as explained in the third ...
15:33:19: [restore_table] Trying to restore data to SYS.TRUNTAB1$$2 15:33:20: [restore_table] Expected Records in this round: 411 15:33:20: [restore_table] 411 records recovered 此处省略N行输出.....
Data-Unit_MongoDB_Restore适用于磁盘存储故障、勒索病毒删除、误操作删除等导致的数据库丢失恢复。运行工具后将数据文件拖入本窗口即可。也支持扫描分区和裸磁盘,建议扫描磁盘镜像文件,效果更好。 免费版每个集合...
Data Loss Management and Prevention Techniques What You’ll Learn Be instructed on backup and restore techniques and principles Understand the importance of a fully implemented backup plan in SQL ...
* After installing the patch and Firefox 3.6 on a computer with UAC (User Account Control) enabled, you need to start QuickTest at least one time using the “Run as administrator” mode. ...
群晖restore工具
恢复删除(delete) 操作的数据,通过日志文件 Restore delete of data from sqlserver
Clonezilla for Win 8 and 8.1 Backup and Restore
支持从裸磁盘、分区、文件中搜索所有IBD page碎片。 1、输入字母a-z :搜索对应的逻辑分区A-Z。此模式请以管理员身份运行。 2、输入数字1-9 :搜索物理磁盘,对应“磁盘管理器”中的磁盘0-9。...
This book presents a systematic and unified approach for modern nonparametric treatment of missing and modified data via examples of density and hazard rate estimation, nonparametric regression, ...
网络数据还原代码,自己上传看看,临时保存
tsm restore命令的详细解释! Therestorecommandobtainscopiesofbackupversionsofyourfiles from aTivoli®StorageManagerserver,orinside abackup set. Torestore files, specifythe directoriesorselectedfiles, ...
Java&C++ SDK for FusionSphere Backup&Restore
tensorflow model save and restore example
restore-symbol
LCM(st7701s) 480800 量产驱动,MTK ,MT6580 mtk平台可参考
RESTORE DATABASE命令还原SQLServer 2005 数据库
[量产部落]Restore_v3.26.0.0