1 year ago
#386719
Surabhi ojha
ActiveRecord methods(find_by, find etc) not working when using use_transactional_fixtures = true in rspec
I am trying to move from use_transactional_fixtures = false
to use_transactional_fixtures = true
on my ROR application.
Many cases are failing after setting use_transactional_fixtures = true in RSpec config.
On further digging I came to find that the Active record query were not reading from transactions.
Example
before(:all) do
@user = User.first
end
...
describe 'POST #create' do
context 'with valid params' do
it 'creates a new HighScore' do
post :create, high_score: { game: 'Test Game', score: 200, user_id: @user.id }
parsed_response = JSON.parse(response.body)
expect(@user.high_scores.find_by(id: parsed_response[:highscore_id])).to be_instance_of(HighScore)
end
end
end
users table data before test starts
id | first_name | last_name | |
---|---|---|---|
1 | Test | Account | test123@gmail.com |
high_scores table before test starts
id | game | score | user_id |
---|---|---|---|
1 | One | 100 | 1 |
The issue with this is @user.high_scores.find_by(id: parsed_response[:highscore_id])
returns
nil
But the expected response is:-
#<HighScore id: 2, game: "Test Game", score: 200, created_at: "2022-04-11 10:36:39", updated_at: "2022-04-11 10:36:39", user_id: 1>
HighScore.all returns the value which is persisted in database before the test started i.e
#<HighScore id: 1, game: "One", score: 100, created_at: "2022-04-07 10:36:39", updated_at: "2022-04-07 10:36:39", user_id: 1>
The controller methods are all working and there is no issue with respect to creation of object. These cases were working fine when transactional_fixtures were set to false.
Versions
Rails 4.2.11.3
ruby 2.6.9
RSpec 3.7
- rspec-core 3.7.1
- rspec-expectations 3.7.0
- rspec-mocks 3.7.0
- rspec-rails 3.7.2
- rspec-support 3.7.1
I need someone to help me out on what is the cause that queries are not getting executed on transactions but rather on tables.
ruby-on-rails
ruby
rspec
transactions
rails-activerecord
0 Answers
Your Answer