Skip to content
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

Can't test enums which aren't backed by a column #1457

Closed
Kutomore opened this issue Jul 25, 2021 · 0 comments · Fixed by #1464
Closed

Can't test enums which aren't backed by a column #1457

Kutomore opened this issue Jul 25, 2021 · 0 comments · Fixed by #1464

Comments

@Kutomore
Copy link
Contributor

Due to how the code on https://github.com/thoughtbot/shoulda-matchers/blob/c934d7534520d90cf9d17f0cd1745bf95d38cf41/lib/shoulda/matchers/active_record/define_enum_for_matcher.rb#L234 is implemented, if you have a enum which is not backed by a column in your database you'll get an error when trying to run the test.

I've ran into this when creating a enum to match a field I added to my query through SQL only, which in teory is not saved on my database.

To reproduce simply create a enum that has no field on the database with the same name, and you'll get the error.

# frozen_string_literal: true

class Car < ApplicationRecord
  enum label: { perfect_match: 0, good_match: 1 }
end

describe Car do
  it { is_expected.to define_enum_for :label }
end
  1) Car is expected to define :label as an enum backed by an integer
     Failure/Error: it { is_expected.to define_enum_for :label }

     NoMethodError:
       undefined method `type' for nil:NilClass
     # /usr/local/bundle/gems/shoulda-matchers-5.0.0/lib/shoulda/matchers/active_record/define_enum_for_matcher.rb:363:in `column_type_matches?'
     # /usr/local/bundle/gems/shoulda-matchers-5.0.0/lib/shoulda/matchers/active_record/define_enum_for_matcher.rb:234:in `matches?'
     # ./spec/models/car_spec.rb:3:in `block (2 levels) in <main>'

I personally believew the type of the enum should only be checked if backed_by_column_of_type is called.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant