Mad*_*hik 2 database testing persistence controller ruby-on-rails
我正在使用ActionController :: TestCase(下面的代码)测试BranchController的create方法.我通过调用find_by_name方法检查对象是否已创建(假设name在此处是唯一的).测试运行成功,但当我在mysql数据库中检查相同的记录时,它不存在.
class Security::BranchControllerTest < ActionController::TestCase
test "the create" do
post(:create, :branch => {:name => "test branch", :details=> "test branch details"})
#replace find with where searching with all of fields
assert_not_nil Company::Branch.find_by_name("test branch")
end
end
Run Code Online (Sandbox Code Playgroud)
如果您正在使用支持事务的数据库(如今大多数情况下那样),则rails测试将默认在运行每个测试之前设置一个保存点,并在最后执行回滚.
这意味着插入实际上正在发生,但结果在测试之外是不可见的.
您应该在测试日志中看到保存点和回滚操作.
如果要禁用此功能,可以添加
self.use_transactional_fixtures = false
在您的测试类中,或通过添加类似的东西为所有测试修补它
class ActiveSupport::TestCase
self.use_transactional_fixtures = false
end
Run Code Online (Sandbox Code Playgroud)
在你的test_helper类中.
但是,通常禁用此功能可能不是一个好主意,因为这是保持测试独立性的好方法.
| 归档时间: |
|
| 查看次数: |
695 次 |
| 最近记录: |