【发布时间】:2014-08-09 12:49:45
【问题描述】:
我的模拟 stub 正确地得到了 escaped,但是 API 响应
一个没有得到escaped。
下面是我的模拟存根工厂。
require 'faker'
FactoryGirl.define do
factory :account do |f|
f.name {Faker::Name.name}
f.description {Faker::Name.description}
end
factory :account_json, class: OpenStruct do
send :'@type' , "accountResource"
createdAt "2014-08-07T14:31:58"
createdBy "2"
updatedAt "2014-08-07T14:31:58"
updatedBy "2"
accountid "2055"
name "Test"
description "Something about Test"
disabled "false"
end
end
以下是我正在构建工厂存根并尝试将其与 API 响应进行比较的规范。
it "can find an account that this user belongs to" do
account = Account.find( id: 2055, authorization: @auth )
hashed_response = FactoryGirl.build(:account_json).marshal_dump.to_json
expect(account.to_json).to eq(hashed_response.to_json);
end
API 响应和存根
FactoryGirl 存根
expected: "\"{\\\"@type\\\":\\\"accountResource\\\",\\\"createdAt\\\":\\\"2014-08-07T14:31:58\\\",\\\"createdBy\\\":\\\"2\\\",\\\"updatedAt\\\":\\\"2014-08-07T14:31:58\\\",\\\"updatedBy\\\":\\\"2\\\",\\\"accountid\\\":\\\"2055\\\",\\\"name\\\":\\\"Test\\\",\\\"description\\\":\\\"Something about Test\\\",\\\"disabled\\\":\\\"false\\\"}\"
API 响应
got: "{\"@type\":\"accountResource\",\"createdAt\":\"2014-08-07T14:31:58\",\"createdBy\":2,\"updatedAt\":\"2014-08-07T14:31:58\",\"updatedBy\":2,\"accountid\":2055,\"name\":\"Test\",\"description\":\"Something about Test\",\"disabled\":false}"
为什么我通过 Factory 创建的 JSON 与我从 API 获得的 JSON 不同。
【问题讨论】:
标签: ruby-on-rails ruby json ruby-on-rails-4 rspec