rringler
rringler

Reputation: 61

Rails ActiveRecord Serialized Data Rspec Test

I'd like to use rspec to test the validity of a serialized hash, but I'm not really sure how to go about it. Is there a way to verify that hash has specific keys and values?

foo.rb

class Foo < ActiveRecord::Base
  attr_accessor :bar
  serialize :bar

end

foo_spec.rb:

require 'spec_helper'

describe Route do
  before { @foo = Foo.new(bar: { a: 1, b: 2, c: 3 }) }

  subject { @foo }

  it { should #????? }
end

Upvotes: 3

Views: 1672

Answers (1)

Dylan Markow
Dylan Markow

Reputation: 124419

To make sure serialization is actually working, you'd want to create the record, not just initialize it with new (i.e. save it to your database, which will then store it as YAML). Then you could read it back in (using reload to make sure it's actually retrieving the serialized database version):

it "deserializes the hash" do
  @foo.reload.bar.should eq({a: 1, b:2, c: 3})
end

Upvotes: 3

Related Questions