问题描述
我正在使用 ruby-2.5.0 和 rails 5 进行 RoR 项目.我正在为我的 api 使用 jsonapi-serializers.我有一个带有自定义属性的序列化程序,如下所示:-
Hi i am working on a RoR project with ruby-2.5.0 and rails 5. I am using the jsonapi-serializers for my api. I have a serializer with custom attribute as follows:-
class ReceiptPartialSerializer
include JSONAPI::Serializer
TYPE = 'receipt'
attribute :id
attribute :receipt_partials
attribute :receipt_partials do
receipt_container = []
object.receipt_partials.each do |partial|
receipt_partial = {}
receipt_partial['id'] = partial.id
receipt_partial['image_url'] = 'https:' + partial&.picture&.url
receipt_container << receipt_partial
end
receipt_container
end
end
我的这个序列化器的规范文件是:-
My spec file for this serializer is:-
RSpec.describe ReceiptPartialSerializer do
let(:id) { 1 }
let(:image_url) { 'https:/images/original/missing.png' }
let(:receipt_id) { 1 }
let(:receipt_partial) do
ReceiptPartial.new(
receipt_id: receipt_id
)
end
subject { JSONAPI::Serializer.serialize(receipt_partial) }
it { is_expected.to have_jsonapi_attributes('image-url' => image_url) }
end
但是当我运行我的测试时,它没有覆盖我的自定义属性,即receipt_partials.知道如何覆盖我的自定义属性.提前致谢.
But when i run my test it doesnot cover my custom attribute which is receipt_partials.Any idea how can i cover my custom attribute. Thanks in advance.
推荐答案
看起来您只是在测试序列化程序的一个属性 - image-url
.您是否尝试为 receipt_partial
返回的内容添加期望?您可以使用以下方法将收据部分存储在测试中的方法中:
It looks like you're only testing one attribute of your serializer - which is image-url
. Have you tried adding an expectation for what is returned by receipt_partial
? You could store receipt partial in a method in your test by using something like so:
def receipt_partial
@receipt_partial ||= ReceiptPartial.new(receipt_id: receipt_id)
end
然后将您的测试更新为如下所示:
And then update your test to look like this:
describe ReceiptPartialSerializer do
let(:id) { 1 }
let(:image_url) { 'https:/images/original/missing.png' }
let(:receipt_id) { 1 }
let(:receipt_partial) { receipt_partial }
subject { JSONAPI::Serializer.serialize(receipt_partial) }
it { is_expected.to have_jsonapi_attributes('image-url' => image_url) }
it { is_expected.to have_jsonapi_attributes('receipt_partial' => receipt_partial) }
def receipt_partial
@receipt_partial ||= ReceiptPartial.new(receipt_id: receipt_id)
end
end
您需要检查 have_jsonapi_attributes
是否接受一个数组,如果接受,您可以将该期望语句合并为所有序列化程序参数的一个大期望值.
You'll want to check if have_jsonapi_attributes
accepts an array, and if it does, you could consolidate that expectation statement into one large expectation of all of your serializer's arguments.
此外,根据我的回答此处,我相信序列化程序应该在测试时明确期望返回的内容,而不是测试是否包含给定的属性.这是我第一次看到 have_jsonapi_attributes
助手,所以如果这是有效的方式,那就太好了.否则,可能值得调整您对返回内容的明确期望的测试.
Also, per my answer here, I believe serializers should be tested with explicit expectations for what is returned, instead of testing whether a given attribute is included. This is my first time seeing the have_jsonapi_attributes
helper, so if that's the way that works, great. Otherwise, it might be worth adjusting your test for explicit expectations for what is returned.
这篇关于如何使用 rspec 为具有自定义属性的序列化程序编写单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!