Ruby Language
शुद्ध RSpec JSON एपीआई परीक्षण
खोज…
Serializer ऑब्जेक्ट का परीक्षण करना और उसे नियंत्रक से परिचित कराना
मान लीजिए कि आप jsonapi.org विनिर्देशन का अनुपालन करने के लिए अपना एपीआई बनाना चाहते हैं और परिणाम इस तरह दिखना चाहिए:
{
"article": {
"id": "305",
"type": "articles",
"attributes": {
"title": "Asking Alexandria"
}
}
}
सीरियल की वस्तु के लिए परीक्षण इस तरह लग सकता है:
# spec/serializers/article_serializer_spec.rb
require 'rails_helper'
RSpec.describe ArticleSerializer do
subject { described_class.new(article) }
let(:article) { instance_double(Article, id: 678, title: "Bring Me The Horizon") }
describe "#as_json" do
let(:result) { subject.as_json }
it 'root should be article Hash' do
expect(result).to match({
article: be_kind_of(Hash)
})
end
context 'article hash' do
let(:article_hash) { result.fetch(:article) }
it 'should contain type and id' do
expect(article_hash).to match({
id: article.id.to_s,
type: 'articles',
attributes: be_kind_of(Hash)
})
end
context 'attributes' do
let(:article_hash_attributes) { article_hash.fetch(:attributes) }
it do
expect(article_hash_attributes).to match({
title: /[Hh]orizon/,
})
end
end
end
end
end
धारावाहिक वस्तु इस तरह दिख सकती है:
# app/serializers/article_serializer.rb
class ArticleSerializer
attr_reader :article
def initialize(article)
@article = article
end
def as_json
{
article: {
id: article.id.to_s,
type: 'articles',
attributes: {
title: article.title
}
}
}
end
end
जब हम अपने "धारावाहिक" चलाते हैं तो ऐनक सब कुछ गुजर जाता है।
यह बहुत उबाऊ है। आइए हमारे लेख धारावाहिक के लिए एक टाइपो का परिचय दें: type: "articles" बजाय type: "articles" चलो वापसी type: "events" और हमारे परीक्षणों को फिर से चलाएँ।
rspec spec/serializers/article_serializer_spec.rb
.F.
Failures:
1) ArticleSerializer#as_json article hash should contain type and id
Failure/Error:
expect(article_hash).to match({
id: article.id.to_s,
type: 'articles',
attributes: be_kind_of(Hash)
})
expected {:id=>"678", :type=>"event",
:attributes=>{:title=>"Bring Me The Horizon"}} to match {:id=>"678",
:type=>"articles", :attributes=>(be a kind of Hash)}
Diff:
@@ -1,4 +1,4 @@
-:attributes => (be a kind of Hash),
+:attributes => {:title=>"Bring Me The Horizon"},
:id => "678",
-:type => "articles",
+:type => "events",
# ./spec/serializers/article_serializer_spec.rb:20:in `block (4
levels) in <top (required)>'
एक बार जब आप परीक्षण चला लेते हैं तो त्रुटि को हल करना बहुत आसान है।
एक बार जब आप त्रुटि को ठीक कर लेते हैं ( article के प्रकार को सही article ) तो आप इसे इस तरह नियंत्रक से मिलवा सकते हैं:
# app/controllers/v2/articles_controller.rb
module V2
class ArticlesController < ApplicationController
def show
render json: serializer.as_json
end
private
def article
@article ||= Article.find(params[:id])
end
def serializer
@serializer ||= ArticleSerializer.new(article)
end
end
end
यह उदाहरण लेख पर आधारित है: http://www.eq8.eu/blogs/30-pure-rspec-json-testing
Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow