【问题标题】:Rails ActiveRecord Serialized Data Rspec TestRails ActiveRecord 序列化数据 Rspec 测试
【发布时间】:2012-02-23 04:01:59
【问题描述】:

我想使用 rspec 来测试序列化哈希的有效性,但我不确定如何去做。有没有办法验证哈希是否具有特定的键和值?

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

【问题讨论】:

    标签: ruby-on-rails ruby rspec


    【解决方案1】:

    为了确保序列化确实有效,您需要create 记录,而不仅仅是使用new 对其进行初始化(即,将其保存到您的数据库中,然后将其存储为 YAML)。然后你可以读回它(使用reload 确保它实际上是在检索序列化的数据库版本):

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

    【讨论】:

    • 谢谢;我在我的前块中添加了一个@foo.save,并且能够在我的测试中重新加载@foo 实例。似乎现在可以工作了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-03
    • 2021-05-17
    • 1970-01-01
    • 2012-07-28
    • 2015-04-04
    • 1970-01-01
    • 2023-03-17
    相关资源
    最近更新 更多