Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 407
rringler Avatar asked Dec 20 '25 18:12

rringler


1 Answers

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
like image 71
Dylan Markow Avatar answered Dec 22 '25 07:12

Dylan Markow



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!