shoulda-matchers: `cast_type` use in rails_shim causes a problem with the serialize matcher - Rails 5

I have seen this issue in Rails 5, and shoulda-matchers 3.1.1 (along with pulling shoulda-matchers directly from master).

IntakeRequest should serialize :payload class_name => JSON
     Failure/Error: should serialize(:payload).
     NoMethodError:
       undefined method `cast_type' for #<ActiveRecord::ConnectionAdapters::MySQL::Column:0x007f8dcc21d778>

This cast_type attribute has been removed from ActiveRecord::ConnectionAdapters::MySQL::Column per this commit

The cast_type attribute is called from within the rails_shim

I would be happy to help fix this, but could use some direction if anyone has suggestions on how best to address this.

Note: An issue was created by the cast_type change on the oracle adapter that may have useful information as they come to a resolution.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 9
  • Comments: 28 (2 by maintainers)

Commits related to this issue

Most upvoted comments

@adammcfadden I had to make a small change with respect to shoulda master, and it works a treat. Thanks!

# spec/rails_helper.rb
module Shoulda
  module Matchers
    RailsShim.class_eval do
      def self.serialized_attributes_for(model)
        if defined?(::ActiveRecord::Type::Serialized)
          # Rails 5+
          model.columns.select do |column|
            model.type_for_attribute(column.name).is_a?(::ActiveRecord::Type::Serialized)
          end.inject({}) do |hash, column|
            hash[column.name.to_s] = model.type_for_attribute(column.name).coder
            hash
          end
        else
          model.serialized_attributes
        end
      end
    end
  end
end

@mcmire unfortunately, yes. I’m using rspec to test a serialization field on my model but I’m facint some errors on those tests:

My model make.rb

class Make < ApplicationRecord
  serialize :vehicle_types, Array
end

When I run the example it { is_expected.to serialize(:vehicle_types).as(Array) }

I got this error:

NoMethodError:
       undefined method `cast_type' for #<ActiveRecord::ConnectionAdapters::PostgreSQLColumn:0x0000564663634340>

In my Gemfile.lock I have shoulda-matchers (3.1.2)

I merged @bsodmike’s fix into the rails-5 branch in 28a43a3b6081deb65dc1a25a5cd6f9608728c467, so I’m going to close this to clean things up. We’ll have a new pre-release out soon so people can use that instead of having to use a branch in their Gemfiles.

Even a .beta or .rc1 version would be very much appreciated. 😃

I can confirm this works perfectly on master.

A quick note to others, ensure you’re following the master branch of this repository:

# Gemfile
gem "shoulda-matchers", git: "https://github.com/thoughtbot/shoulda-matchers"

@mcmire thanks again for the patch! It would be great to get this into an official release soon.

When will this be fixed and released?

Yep it would be nice to get a new version of shoulda-matchers out, last update was over a year ago.

https://rubygems.org/gems/shoulda-matchers/versions/3.1.2

For the record, it looks like 4.0.0.rc1 was released in Oct with Rails 5 support.

@mcmire Any progress on a pre-release? We’re still using the branch.