Reputation: 61
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
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