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 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With