-
Notifications
You must be signed in to change notification settings - Fork 483
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Records created in before(:all) are cleared within examples. #564
Comments
This also, perhaps unsupprisingly, also happens with the deletion strategy. |
hi, yes it's an expected behaviour, transaction strategy requires a block and it'll clean only records created within the block
on the other hand truncation and deletion strategy just clean the whole database except persistant tables
it's basically the same as this
|
It's not working as described however.
The output mostly is:
So with the new version all data is being wiped constantly, and our entire seed that we setup when the test-suite starts is removed. |
@coding-bunny Hmm, yes, it does sound like the 1.8 release introduced a regression. If I can reproduce it, I can fix it! I tried to reproduce the error you're experiencing by taking what you shared and chopping it down to just ActiveRecord and DatabaseCleaner here: https://gist.github.com/botandrose/5dc4c20e5f3d63cee191cbf336f3a542 , but the test passes. Can you try to reduce your test suite down to a minimal failing example that you can share with me? |
Euh...I can try when i find the time, because this is a client's private source code, with a mySQL and Oracle database setup, so this won't be easy to get outside and visible. |
Yes, perhaps you are right! |
I haven't been able to do much around this, but I can tell it's happening on our MySQL database, and all tests fail that depend on our named seeds, e.g data that's created during the test-suite initialization, at the beginning of the RSpec configuration block. It seems that once we define the before/after configuration for database_cleaner, all data is being wiped. |
This is what we have inside the configuration:
We basically generated data with the And all tests fail with the named seed usage. |
Good job narrowing it down to MySQL and not Oracle! That's really good news because it makes it more likely that we'll be able to reduce this down to a test that fails on CI! |
Having kind of the same problem, I'm creating object in rspec I have a minimal setup for now, so I don't need records to be deleted around examples, as per configuration : # spec/spec_helper.rb
RSpec.configure do |config|
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
end I have a shared_context, when included it creates a user. I added debug prompts : # spec/support/shared_contexts/authorization_header.rb
require 'devise/jwt/test_helpers'
shared_context 'authorization_header_swagger' do
byebug # User.count => 6
user = FactoryBot.create :user
byebug # User.count => 7
let(:'Authorization') { Devise::JWT::TestHelpers.auth_headers({}, user)['Authorization'] }
end And finally in the test where I need the user to exist, it is as if the user was never created (back to 6 entities instead of 7) # spec/requests/some_test.rb
context 'with auth" do
include_context 'authorization_header_swagger'
it 'byebug' do
byebug # User.count => 6
end
end gems:
db:
Maybe, as the transaction mode was enabled, I need to flush or commit after creating any objects in contexts ? |
@Ex-Ark thank you for sharing this example! It looks relatively simple, so I think we should be able to pare this down to a failing test. Is it possible for you to share this in a github repo that reproduces the problem? If you can do that, I'd be happy to take it from there! |
Sure thing, here is the most basic repo I could create to reproduce what I mentionned in my comment. From there you can add before(:all) in tests to try OP's setup. |
Hi @Ex-Ark! Thank you so much for sharing this repo. It's super helpful to be able to look at the same code to figure out what's going on! After taking a look, I think that the bug is actually in how the test is using RSpec's shared context. Specifically, the user creation code is happening directly in the # incorrect
shared_context 'a_user_exist' do
user = FactoryBot.create :user
end
# correct
shared_context 'a_user_exist' do
before { user = FactoryBot.create :user }
end When I change the code in the repo to the second version, the test passes. What do you think? Is this indeed the culprit, or maybe I've missed something? I wasn't able to modify the example with Oh, and here are the RSpec docs for Thoughts? |
Thanks for taking a look at this ! Anyway I refactored my context using your approach, in fact I declared everything in Strange thing is that I used (wrongly ? 👎 ) rspec context until now and it always worked before I included database_cleaner into my test suite. Not sure if it applies to OP's problem, but it is fixed for my case ! # what I had
shared_context 'authorization_header_swagger' do
user = FactoryBot.create(:user)
let(:'Authorization') { Devise::JWT::TestHelpers.auth_headers({}, user )['Authorization'] }
end
# what I have now
shared_context 'authorization_header_swagger' do
let(:'Authorization') { Devise::JWT::TestHelpers.auth_headers({}, FactoryBot.create(:user))['Authorization'] }
end I did not try using |
@Ex-Ark you're welcome! I'm glad we were able to resolve your issue. Since it didn't ultimately involve |
@botandrose I am facing the same issue as the author of this PR @sevab And I took a time to write a very simple way to reproduce the issue The issueI am facing a very anonying issue with the When using the Please take a look at the simple code snippet below... require 'rails_helper'
RSpec.describe Record, type: :model do
describe 'when creating a Project' do
before(:all) do
Project.delete_all
@project = Project.create(name: 'Test')
end
it 'should have the Project created' do
expect(Project.count).to be_eql(1)
end
it 'should still have the Project created' do
expect(Project.count).to be_eql(1)
end
it 'should also still have the Project created' do
expect(Project.count).to be_eql(1)
end
end
end ☝️
Before I applied the I had this line on the version before applying the require 'rails_helper'
RSpec.describe Record, type: :model do
describe 'when creating a Project' do
before(:all) do
Project.delete_all # Here is the change!
@project = Project.create(name: 'Test')
end
it 'should have the Project created' do
expect(Project.count).to be_eql(1)
end
it 'should still have the Project created' do
expect(Project.count).to be_eql(1)
end
it 'should also still have the Project created' do
expect(Project.count).to be_eql(1)
end
end
end The configurationI configured my RSpec.configure do |config|
# Configure DatabaseCleaner to automatically reset the database between tests
config.before(:suite) do
DatabaseCleaner[:mongoid].strategy = :truncation
end
config.around(:each) do |example|
DatabaseCleaner.cleaning do
example.run
end
end
end How can I solve this?If I change the |
I recently switched our config from transactional to truncation strategy as shown in the Rspec + Capybara example in Readme.
However, this seems to affect the data created in the
before(:all)
. With transaction strategy, records created inbefore(:all)
were persisted and available between examples, as expected. With truncation strategy, however, records created withinbefore(:all)
are only available in the first test, and are then removed.Is this an expected behaviour/side-effect of transactional strategy?
The text was updated successfully, but these errors were encountered: