The plus point of having test environment is that we can test our whole application in a different dedicated environment without any manual efforts. Technically, it involves views, controller methods, request/response, routes, models(callbacks, validations, etc.)
There are few scenarios in which we'd require to test after_commit changes.The application which manipulates data majorly in after_commit callback could cause issues in test environment thus causing it to fail.
The problem is that the after_commit callback is never fired in rspec if we use transactional fixtures.
-> What is transactional_fixtures ?
-> ActiveRecord’s fixtures, will, by default, rollback everything that was done on the database during the method in question, unless you set (self.use_transactional_fixtures = false). Herein, if active_record is rolledback, after_commit isn't fired.
In case, if we desire to use transactional fixtures, there are multiple ways to achieve this:
- We need to trigger after_commit callback manually via ar_object.run_callbacks(:after_commit)
- There are gems which would hook up the after_commit callback as soon as transaction is committed. Bundling this gem would do run after_commit callback automatically without any manual trigger.
Gems like [ar_after_transaction] ,
[test_after_commit] , etc.
Here is the blog which further gives other ways to achieve this: