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

with_options used with define_enum_for #1654

Open
tapalilov opened this issue Oct 11, 2024 · 0 comments
Open

with_options used with define_enum_for #1654

tapalilov opened this issue Oct 11, 2024 · 0 comments

Comments

@tapalilov
Copy link

tapalilov commented Oct 11, 2024

Description

When ActiveSupport’s with_option is loaded, and it mistakenly used instead of with_values, the define_enum_for test still passes even for wrong enum value and shows an incorrect message:

Post
  validations
    is expected to define :status as an enum backed by an integer, prefix: {:draft=>0, :non_existing=>1}

Reproduction Steps

require 'bundler/inline'

gemfile(true) do
  source 'https://rubygems.org'
  gem 'shoulda-matchers'
  gem 'activerecord'
  gem 'activesupport'
  gem 'sqlite3'
  gem 'rspec'
end

require 'active_record'
require 'active_support/core_ext/object/with_options'
require 'shoulda-matchers'
require 'logger'

ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)

ActiveRecord::Schema.define do
  create_table :posts, force: true do |t|
    t.string :body
    t.integer :status
  end
end

Shoulda::Matchers.configure do |config|
  config.integrate do |with|
    with.test_framework :rspec

    with.library :active_record
    with.library :active_model
  end
end

RSpec.configure do |config|
  config.include Shoulda::Matchers::ActiveRecord
  config.include Shoulda::Matchers::ActiveModel
  config.include Shoulda::Matchers::ActionController
end

class Post < ActiveRecord::Base
  enum :status, { draft: 0, completed: 1}, prefix: true
end

RSpec.describe Post do
  describe 'validations' do
    it do
      is_expected
        .to define_enum_for(:status).with_options(draft: 0, non_existing: 1).with_prefix
    end
  end
end

Expected behavior

Raise exception for undefined method

Actual behavior

Returns green spec even with enum checked with wrong value.
Screenshot 2024-10-11 at 15 09 19

System configuration

shoulda_matchers 6.4.0:
rails 7.1.x:
ruby 3.2.x:

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

No branches or pull requests

1 participant