Rails数据库测试和清理
让我们想象一下:
class ModTest < ActiveSupport::TestCase
test "something" do
m1 = Mod.new
# test some things
assert m1.save
end
test "whatever" do
m2 = Mod.new
# test other things
assert m2.save
end
end
在执行第二个测试用例(即 whatever
)之前,数据库会被清除,还是会包含第一个测试用例添加的对象? 这种行为可以控制/定制吗?
Let's imagine this:
class ModTest < ActiveSupport::TestCase
test "something" do
m1 = Mod.new
# test some things
assert m1.save
end
test "whatever" do
m2 = Mod.new
# test other things
assert m2.save
end
end
Before the 2nd test case gets executed, the one called whatever
, will the database be cleared, or will it contain the object added by the first test case?
Can this behaviour be controlled/customised?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不能 100% 确定默认行为是什么,我一直在使用 database_cleaner gem 来实现此目的。以下是我的
spec_helper.rb
中的相关代码:需要注意的是,如果您选择此路线,请确保取出默认
spec_helper 中的
如果您使用事务清理策略 - 将其设置为 true 会导致事务内事务错误(至少对于 sqlite 数据库而言)。config.use_transactional_fixtures
行.rbNot 100% sure on what the default behavior is, I've been using the database_cleaner gem for this purpose. Below is the relevant code in my
spec_helper.rb
:One caveat, if you go this route make sure you take out the
config.use_transactional_fixtures
line in the defaultspec_helper.rb
if you use the transaction cleaning strategy - leaving it set to true causes transaction within transaction errors (at least for sqlite databases).