【问题标题】:RSpec Rails Serializer JSONRSpec Rails 序列化器 JSON
【发布时间】:2013-09-10 13:30:42
【问题描述】:

我正在尝试为我的 rails 应用程序中的序列化程序编写一个简单的 Rspec。我尝试使用:

ActiveModelSerializer::Matchers。因为这不起作用。我确实尝试让我的 Rspec 测试如下所示:

require 'spec_helper'

describe CoffeeTypeSerializer do

  it { should have_key(:name) }
  it { should have_key(:image) }
  it { should have_key(:type_of_coffee) }
  it { should have_key(:temp) }
  it { should have_key(:caffeinated) }
  it { should have_key(:basicInfo) }
  it { should have_key(:price) }
  it { should have_key(:created_at) }
  it { should have_key(:updated_at) }
  it { should have_key(:fullInfo) }
  it { should have_key(:drinks) }
end

不幸的是没有运气并得到以下错误:

ArgumentError:参数数量错误(0 表示 1..2)

尝试搜索高低,但测试 JSON 序列化程序的最佳方法是什么

【问题讨论】:

    标签: ruby-on-rails json rspec


    【解决方案1】:

    我们已经完成并基本上推出了我们自己的匹配器,因为 JSON 序列化器也不适用于我们。他们有点参与,所以我将它们添加到一个要点中:https://gist.github.com/gavingmiller/e03ff13edfeef8d5c08d

    示例测试最终看起来像这样:

    require 'spec_helper'
    
    describe FeedSerializer, type: :serializer do
      it { should have_attribute(:id) }
      it { should have_attribute(:name) }
      it { should have_many_relation(:feed_filters) }
    end
    

    当我们对序列化程序进行修改(即:与直接属性不同的输出)时,我们的测试如下所示:

    it "successfully parses JSON as an additional field" do
      @data = '{ "key": "value" }'
      json = FooBarSerializer.new(data).as_json
      json[:foo][:bar]['key'].should == 'value'
    end
    

    【讨论】:

    • 有没有机会让这些匹配器成为宝石?
    猜你喜欢
    • 1970-01-01
    • 2021-03-28
    • 1970-01-01
    • 2011-07-05
    • 2020-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-17
    相关资源
    最近更新 更多