响应主体未被序列化
Response body not being serialized
我有一个 ActiveModel 序列化程序,可以将字段名称更改为 lowerCamelCase。但是当我尝试在 rspec
上测试它时,它在我的 response.body 上不起作用
ActiveModel::Serializer.setup do |config|
config.key_format = :lower_camel
end
class DevelopmentAgentsSerializer < ActiveModel::Serializer
attributes :id, :name, :email, :created_at, :updated_at, :phone
end
class DevelopmentAgentsController < ApplicationController
def index
@development_agents = DevelopmentAgent.all
render json: @development_agents
end
end
it "returns a list of development agents" do
get :index, format: :json
expect(JSON.parse(response.body)).to eq(JSON.parse({development_agents: serialized_development_agent}.to_json))
end
预期:{"development_agents"=>[{"id"=>3, "name"=>"Some name", "email"=>nil, "createdAt"=>"2019-08-06T17:30:47.372-03:00", "updatedAt"=>"2019-08-06T17:30:47.372-03:00", "phone"=>"(21)999999999"}]}
得到:
{"development_agents"=>[{"id"=>3, "name"=>"Some name", "email"=>nil, "created_at"=>"2019-08-06T17:30:47.372-03:00", "updated_at"=>"2019-08-06T17:30:47.372-03:00", "phone"=>"(21)999999999"}]}
按照惯例,序列化程序的名称是单数的,即 DevelopmentAgentSerializer
而不是 DevelopmentAgentsSerializer
(不要忘记也更改文件名)。如果不遵循此约定,将不会使用您定义的序列化程序,并且响应将只是 @development_agents.as_json
我有一个 ActiveModel 序列化程序,可以将字段名称更改为 lowerCamelCase。但是当我尝试在 rspec
上测试它时,它在我的 response.body 上不起作用ActiveModel::Serializer.setup do |config|
config.key_format = :lower_camel
end
class DevelopmentAgentsSerializer < ActiveModel::Serializer
attributes :id, :name, :email, :created_at, :updated_at, :phone
end
class DevelopmentAgentsController < ApplicationController
def index
@development_agents = DevelopmentAgent.all
render json: @development_agents
end
end
it "returns a list of development agents" do
get :index, format: :json
expect(JSON.parse(response.body)).to eq(JSON.parse({development_agents: serialized_development_agent}.to_json))
end
预期:{"development_agents"=>[{"id"=>3, "name"=>"Some name", "email"=>nil, "createdAt"=>"2019-08-06T17:30:47.372-03:00", "updatedAt"=>"2019-08-06T17:30:47.372-03:00", "phone"=>"(21)999999999"}]}
得到:
{"development_agents"=>[{"id"=>3, "name"=>"Some name", "email"=>nil, "created_at"=>"2019-08-06T17:30:47.372-03:00", "updated_at"=>"2019-08-06T17:30:47.372-03:00", "phone"=>"(21)999999999"}]}
按照惯例,序列化程序的名称是单数的,即 DevelopmentAgentSerializer
而不是 DevelopmentAgentsSerializer
(不要忘记也更改文件名)。如果不遵循此约定,将不会使用您定义的序列化程序,并且响应将只是 @development_agents.as_json