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
To make sure serialization is actually working, you’d want to
createthe record, not just initialize it withnew(i.e. save it to your database, which will then store it as YAML). Then you could read it back in (usingreloadto make sure it’s actually retrieving the serialized database version):