-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
build_stubbed broke when i upgrade to Rails 7.1.3 #1634
Comments
We experience the same. After updating Rails from 7.0 to 7.1 it seems that uniqueness validations of models instantiated with Example:Consider the following spec, asserting that users with non-unique usernames are invalid: # Model
class User < ApplicationRecord
# To reproduce the issue, the model table must also have a unique index on username (see later why)
validates :username, uniqueness: true
end
# Spec
it 'prevents the creation of multiple users with the same username' do
# First create a user with a given username
create(:user, username: 'someone')
# Then, assert that another user with the same username would be invalid
user = build_stubbed(:user, username: 'someone')
expect(user).not_to be_valid
end The spec used to pass in Rails 7.0, but is breaking with Rails 7.1, because I think that this is caused by Rails 7.1 treating persisted records differently in uniqueness validations: in case the record is persisted (and This is an optimization to avoid hitting the database when unnecessary (introduced in Rails 7.1 by this pull request), but it has the side effect of skipping the uniqueness validations for models created by If that's correct, this is not really a bug with Regarding options to "fix" this on |
Thx for the reply @lucaong, we ended up just replacing it with the build |
Reasoning on what My (erroneous) mental model of There is the need for a currently missing method that builds a new model stubbing all the associations, but without persisting not pretending to persist. Basically, a method doing the same as In my ideal scenario,
This would involve changing the current documented behavior of What do the maintainers think about this? If you agree this would be a good feature, and a general direction is agreed (breaking change with cleaner naming vs. non-breaking change with slightly misleading naming), I could hopefully devote some time to send a pull request. |
Description
When i try to upgrade to Rails 7.1 from Rails 6.0, i get some errors in my specs, i found for some reason when i use build_stubbed in validations, it don't work properly.
Reproduction Steps
I just runned my specs and for some reason this broke:
idk, for some reason this dont work, is expected to the invalid_seasonality_item to be invalid, but, its valid in this tests, so, i just changed the
build_stubbed
forbuild
and thats worksExpected behavior
I want some response about what happened with build_stubbed and a solution for that
Actual behavior
At the moment, my specs with build_stubbed are broke.
System configuration
factory_bot version:
"factory_bot_rails", "~> 6.4.0"
rails version:
"rails", "~> 7.1.3"
ruby version:
ruby 3.3.0
The text was updated successfully, but these errors were encountered: