Bruno Mucelini Mergen
Bruno Mucelini Mergen

Reputation: 301

How to test serialize column?

I have in my model this:

class MyModel < ActiveRecord::Base
 serialize :my_column, Array
end

How do I test it?

Today I'm testing this:

it "column serialize Array" do
 subject.my_column.is_a?(Array).must_equal true
end

I'm using gem "minitest-rails-shoulda"

Is there another way to test this?

Tanks

Upvotes: 2

Views: 1300

Answers (2)

michelegera
michelegera

Reputation: 480

You can do it with shoulda-matchers.

class MyModelTest < ActiveSupport::TestCase
  should serialize(:my_column)
end

Check out the code comments, everything should be pretty straightforward.

If you are on Rails 5, watch out as there currently is an issue.

Upvotes: 2

Tal
Tal

Reputation: 7997

serialize stores an object in the db, so you want to test that you can reload after saving (the YAML transformation is successful). do something like this:

it "column serialize Array" do
  obj = MyModel.new 
  obj.my_column = [1,2,3]
  obj.save!
  obj.reload.my_column.should == [1,2,3]
end

Upvotes: 0

Related Questions