RSpec 检查正确的序列化 - 结果包装在数组中序列化
RSpec check of correct serialization - result wraps serialized in an array
我想检查端点的响应是否正确序列化(我使用的是快速 JSON API 序列化程序)。这是我的代码:
endpoint
get do
::Journeys::JourneyListSerializer.new(Journey.where(is_deleted: false))
end
JourneyListSerializer
:
module Journeys
class JourneyListSerializer
include FastJsonapi::ObjectSerializer
attribute :content do |object|
object.content_basic.dig('entity')
end
end
end
my specs
:
let!(:journey) { create(:journey, :with_activities, content_basic: hash) }
let(:hash) {{ "environment": "master", "entity_type": "item", "event_type": "update", "entity": { "id": "5202043", "type": "item", "attributes": { "title": "New text" }}}}
it 'serializes journey with proper serializer' do
call
expect(JSON.parse(response.body)).to be_serialization_of(journey, with: ::Journeys::JourneyListSerializer)
end
be_serialization_of support/matchers
中的帮手
def be_serialization_of(object, options)
serializer = options.delete(:with)
raise 'you need to pass a serializer' if serializer.nil?
serialized = serializer.new(object, options).serializable_hash
match(serialized.as_json.deep_symbolize_keys)
end
整个规格给我一个错误:
1) API::V1::Journeys::Index when params are valid serializes journey with proper serializer
Failure/Error: expect(JSON.parse(response.body)).to be_serialization_of(journey, with: ::Journeys::JourneyListSerializer)
expected {"data"=>[{"attributes"=>{"content"=>{"attributes"=>{"title"=>"New text"}, "id"=>"5202043", "type"=>"item"}}, "id"=>"75", "type"=>"journey_list"}]} to match {:data=>{:id=>"75", :type=>"journey_list", :attributes=>{:content=>{:id=>"5202043", :type=>"item", :attributes=>{:title=>"New text"}}}}}
Diff:
@@ -1,2 +1,2 @@
-:data => {:attributes=>{:content=>{:attributes=>{:title=>"New text"}, :id=>"5202043", :type=>"item"}}, :id=>"75", :type=>"journey_list"},
+"data" => [{"attributes"=>{"content"=>{"attributes"=>{"title"=>"New text"}, "id"=>"5202043", "type"=>"item"}}, "id"=>"75", "type"=>"journey_list"}],
我不知道为什么结果在数组中,如何避免?当我使用此端点时,它在没有数组的情况下进行了序列化。
您在此处测试两种不同类型的输入。控制器正在测试 Journey
的列表,但您的测试只序列化一个。你的匹配器很好,你只是输入错误。
get do
// "where" always returns an array of journeys
::Journeys::JourneyListSerializer.new(Journey.where(is_deleted: false))
end
// "journey" in the test is just one object, so you need to pass an array in the test
expect(JSON.parse(response.body)).to be_serialization_of([journey], with: ::Journeys::JourneyListSerializer)
我想检查端点的响应是否正确序列化(我使用的是快速 JSON API 序列化程序)。这是我的代码:
endpoint
get do
::Journeys::JourneyListSerializer.new(Journey.where(is_deleted: false))
end
JourneyListSerializer
:
module Journeys
class JourneyListSerializer
include FastJsonapi::ObjectSerializer
attribute :content do |object|
object.content_basic.dig('entity')
end
end
end
my specs
:
let!(:journey) { create(:journey, :with_activities, content_basic: hash) }
let(:hash) {{ "environment": "master", "entity_type": "item", "event_type": "update", "entity": { "id": "5202043", "type": "item", "attributes": { "title": "New text" }}}}
it 'serializes journey with proper serializer' do
call
expect(JSON.parse(response.body)).to be_serialization_of(journey, with: ::Journeys::JourneyListSerializer)
end
be_serialization_of support/matchers
def be_serialization_of(object, options)
serializer = options.delete(:with)
raise 'you need to pass a serializer' if serializer.nil?
serialized = serializer.new(object, options).serializable_hash
match(serialized.as_json.deep_symbolize_keys)
end
整个规格给我一个错误:
1) API::V1::Journeys::Index when params are valid serializes journey with proper serializer
Failure/Error: expect(JSON.parse(response.body)).to be_serialization_of(journey, with: ::Journeys::JourneyListSerializer)
expected {"data"=>[{"attributes"=>{"content"=>{"attributes"=>{"title"=>"New text"}, "id"=>"5202043", "type"=>"item"}}, "id"=>"75", "type"=>"journey_list"}]} to match {:data=>{:id=>"75", :type=>"journey_list", :attributes=>{:content=>{:id=>"5202043", :type=>"item", :attributes=>{:title=>"New text"}}}}}
Diff:
@@ -1,2 +1,2 @@
-:data => {:attributes=>{:content=>{:attributes=>{:title=>"New text"}, :id=>"5202043", :type=>"item"}}, :id=>"75", :type=>"journey_list"},
+"data" => [{"attributes"=>{"content"=>{"attributes"=>{"title"=>"New text"}, "id"=>"5202043", "type"=>"item"}}, "id"=>"75", "type"=>"journey_list"}],
我不知道为什么结果在数组中,如何避免?当我使用此端点时,它在没有数组的情况下进行了序列化。
您在此处测试两种不同类型的输入。控制器正在测试 Journey
的列表,但您的测试只序列化一个。你的匹配器很好,你只是输入错误。
get do
// "where" always returns an array of journeys
::Journeys::JourneyListSerializer.new(Journey.where(is_deleted: false))
end
// "journey" in the test is just one object, so you need to pass an array in the test
expect(JSON.parse(response.body)).to be_serialization_of([journey], with: ::Journeys::JourneyListSerializer)