shoulda-matchers
shoulda-matchers copied to clipboard
Version 5.2.0 using define_enum_for causes error
Hey 👋 We upgraded to version 5.2.0, and started seeing errors in our build.
This test:
it do
is_expected.to define_enum_for(:status).with_values(
scheduled: 0,
notification_email_sent: 1,
active: 2,
expired: 3,
cancelled: 4,
terminated: 5,
)
end
use to pass, but now throws:
RSpec::Expectations::ExpectationNotMetError: Expected ItemLicenseDiscounting::Record to define :status as an enum
backed by an integer, mapping ‹"scheduled"› to ‹0›,
‹"notification_email_sent"› to ‹1›, ‹"active"› to ‹2›, ‹"expired"› to
‹3›, ‹"cancelled"› to ‹4›, and ‹"terminated"› to ‹5› with no scopes.
(we can't tell which).
Code being tested:
module ItemLicenseDiscounting
class Record < ApplicationRecord
STATUSES = {
scheduled: 0,
notification_email_sent: 1,
active: 2,
expired: 3,
cancelled: 4,
terminated: 5,
}.freeze
enum status: STATUSES
end
Is something wrong with our code, or the update? The test passes 5.1.0
Ops, sorry about that. Thank you for your report. I don't think there is any problem with your code. I'll take a look at it ASAP this weekend.
@ostigley, one question, would it be possible to tell me which version of Ruby and Rails you are using? Thank you!
Thanks! ruby: 2.7.6 rails: 6.1.7
Hey @VSPPedro we think this is actually on our side. You're in the clear!
@ostigley, thank you for the update. I tried to replicate the problem, but with no luck. I noticed that the ShouldaMatchers is using older versions of ruby and rails - 2.7.5 and 6.1.4.4. Now I'm working on updating everything before trying to replicate the error again. Anyway, if later on you think the issue it's in the ShouldaMatchers, please, let me know. Thanks again.
For posterity, the ugrade broke our test because we have this in our model:
# Remove unwanted ActiveRecord enum methods since they bypass AASM rules
statuses.keys.each { |status| undef :"#{status}!" }
When we removed that, 5.2.0 worked fine.
We are going to update our code.
We think the problem is that should-matchers changed how to check the enum is configured from singleton checks to instance method checks, and since we’re removing one set of instance methods, it’s now failing.