Rails Controller testing: Does database state change persist after success of test?
I am testing create method of BranchController using ActionController::TestCase (code below) . I check if object is created by calling find_by_name method(assume name is unique here). test runs succesfully but when i check same r开发者_高级运维ecord in mysql db, its not there.
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
If you're using a database that supports transactions (as most do these days), rails tests will by default set a savepoint before running each test and do a rollback at the end.
This means that the insertion is actually happening, but the result is not visible outside the test.
You should see the savepoint and rollback operations in your test logs.
If you want to disable this, you can add
self.use_transactional_fixtures = false
in your test class, or patch it in for all your tests by adding something like
class ActiveSupport::TestCase
self.use_transactional_fixtures = false
end
in your test_helper class.
It's likely not a good idea to disable this generally though, as it's a nice way to keep your tests independent.
精彩评论