-
-
Notifications
You must be signed in to change notification settings - Fork 374
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
6.3.0: NoMethodError: undefined method '<' for nil:NilClass
when using a factory with traits
#431
6.3.0: NoMethodError: undefined method '<' for nil:NilClass
when using a factory with traits
#431
Comments
We have the same issue too, preventing us from upgrading the lib.
And we get the same error at the factory_bot_rails version: 6.4.0 |
I don't have a full grasp on what's happening, but I have traced the code and this happens when the factory_bot.compile_factory notification is sent from FacyoryBot with a nil class, which occurs when FactoryBot is sends this notification for defined traits. The ActiveRecordValidator class's validate! method processes the event and assumes class will not be nil and throws this exception. |
There is an issue in the newest release with traits: thoughtbot/factory_bot_rails#431 thoughtbot/factory_bot#1600 as we use this library in many projects, I suggest to add a separate group for it in the preset until the bug is fixed, so we can continue to merge the non-major dependencies for this week.
There is an issue in the newest release with traits: thoughtbot/factory_bot_rails#431 thoughtbot/factory_bot#1600 as we use this library in many projects, I suggest to add a separate group for it in the preset until the bug is fixed, so we can continue to merge the non-major dependencies for this week.
Just tried updating to FactoryBot to 6.4.1 and I'm still seeing this issue. The best I can figure out is that it happens the first time we create a factory which has a trait with the same name as an attribute (e.g. |
FWIW, bumping to |
This fixes an conflict between factory_bot and factory_bot_rails: thoughtbot/factory_bot_rails#431
After upgrading to factory_bot 6.4.1 and factory_bot_rails 6.4.0, I'm still seeing the # In the factory definition...
FactoryBot.define do
factory :magazine do
# ...
end
trait :with_latest_issue do
# ...
end
end
# And then in a spec...
create(:magazine, :with_latest_issue) Adding a |
This worked for me too 👍 |
Bumping |
Note that due to a US holiday this week, and the family + travel leading up to and around that, I won't be able to debug this myself until December. But if someone opens a PR with a test, I can merge and release that. |
I cannot replicate the failure mentioned in the opening post with 6.4.1.
Literally the opening post (active trait with active field: diowa/ruby3-rails7-bootstrap-heroku@3c83903), but it works with 6.4.1 I've also tried enum's implicit traits and they worked |
I'm noticing a weird behavior locally, on my first try it fails, on subsequent tries it works. This for factory_bot 6.4.1 and factory_bot_rails 6.4.0, this happens on rails console. |
Thanks for the clarification! Confirmed it happening in the console Replicable on the opening post application:
EDITStill can't replicate on 6.4.1
Do you have something like |
I do have spring, but I'm able to replicate the issue even when I remove it. One difference that I noticed is the rails version.
EDITI created a new rails 6.1.7.6 app but was unable to replicate the issue there, I'll dig deeper into my app in order to better understand if some other gem is affecting this behavior. I tried to remove |
I've also tried to replicate with 6.1.7.6 without success. At the beginning it was failing, but I opened a new bash console and it stopped. I run it the first time with Spring |
This is failing consistently for me locally and in the CI, but what's interesting is that the same factory with the same attributes/traits is being used in multiple tests, but only some of them are failing. I haven't found the rhyme or the reason yet. |
When I tested it out it appeared to be the first time a factory was created with a given trait or set of arguments so it would move around based on test execution order. |
To pass their class to ActiveSuport::Notifications Re-Fix thoughtbot/factory_bot_rails#431
@mike-burns @tagliala #437 is a hot fix, maybe until thoughtbot/factory_bot#1601 is merged, even if #437 is not merged the spec in consistently fails without the patch and might be worth adding as regression avoidance. |
Thank you to @makicamel for stepping up with a second fix, with great tests. This fix is released as 6.4.2. |
Although top level traits (mixins) are allowed according to the docs[1], they appear to be broken in factory bot 6.4.0[2]. We were seeing the following error: NoMethodError: undefined method `<' for nil:NilClass if for_class < ActiveRecord::Base && for_class.primary_key == attribute.name.to_s When running either of the tests that used the `with_expired_confirmation_token` trait: - test/controllers/account/emails_controller_test.rb:99 - test/controllers/users/emails_controller_test.rb:277 Given that we're only using this trait in User factories I've moved it inside the User factory definition so that we can upgrade. [1]: https://thoughtbot.github.io/factory_bot/traits/mixins.html [2]: thoughtbot/factory_bot_rails#431
Although top level traits (mixins) are allowed according to the docs[1], they appear to be broken in factory bot 6.4.0[2]. We were seeing the following error: NoMethodError: undefined method `<' for nil:NilClass if for_class < ActiveRecord::Base && for_class.primary_key == attribute.name.to_s When running the tests that used the `in_organisation` trait. Given that we're only using this trait in User factories I've moved it inside the User factory definition so that we can upgrade. [1]: https://thoughtbot.github.io/factory_bot/traits/mixins.html [2]: thoughtbot/factory_bot_rails#431
Where can we find this 6.4.2 release? The latest tag on GitHub and version on rubygems.org is 6.4.0. |
Sorry, the 6.4.2 release of factory_bot, which factory_bot_rails depends on. If you |
Ah this would actually have been clear if I read the comments above. It works now fine again with |
Description
After upgrading to 6.3.0, specs with traits started failing.
Downgrade to 6.2.0 works.
The problem seems to be triggered here:
factory_bot_rails/lib/factory_bot_rails/factory_validator/active_record_validator.rb
Line 7 in 3346305
for_class
is nil because the payload containsclass: nil
Reproduction Steps
Please checkout the branch https://github.com/diowa/ruby3-rails7-bootstrap-heroku/tree/tmp/factory-bot-rails-issue
and run
bundle exec rspec spec/model/user_spec.rb
Relevant commit: diowa/ruby3-rails7-bootstrap-heroku@3c83903
Expected behavior
Objects being created as 6.2.0
Actual behavior
System configuration
factory_bot_rails version: 6.3.0
factory_bot version: 6.4.0
rails version: 7.1.2
ruby version: 3.2.2
The text was updated successfully, but these errors were encountered: