front and back-end web development, Leeds, UK


Richard's Blog - Design, coding and life in Japan

Richard

Testing output JSON data from a RESTful API with Cucumber

Some of the following is in Japanese so please excuse this a little, but I am setting up an Restful API in Rails that outputs JSON data, I wanted to check the multi-level JSON objects contained specific data.

   シナリオテンプレート:商品オプションをカートに入れる     前提 以下の"商品"は存在しています: 		| code    | title | price |options|	| TEST001 | 商品1   | 3000 |p:把手タイプ:ライン把手(H)+500:バー把手(E);p:扉カラー:ナディカーマイン(NC):ナディアプリコット(NY):ナディマスカット(NM):ナディアクア(NA):ナディホワイト(NW):ナディダーク(ND):ナディナチュラル(NN);p:シンクの位置:右シンクタイプ:左シンクタイプ;p:ガス種:都市ガス13A:都市ガス12A:プロパンガス(LPG);|	| TEST002 | 商品2   | 2000  |p:把手タイプ:ライン把手(H):バー把手(E);p:扉カラー:ナディカーマイン(NC):ナディアプリコット(NY):ナディマスカット(NM):ナディアクア(NA):ナディホワイト(NW):ナディダーク(ND):ナディナチュラル(NN);p:シンクの位置:右シンクタイプ:左シンクタイプ;p:ガス種:都市ガス13A:都市ガス12A:プロパンガス(LPG);|
	| TEST003 | 商品3   | 500   |p:把手タイプ:ライン把手(H):バー把手(E);p:扉カラー:ナディカーマイン(NC):ナディアプリコット(NY):ナディマスカット(NM):ナディアクア(NA):ナディホワイト(NW):ナディダーク(ND):ナディナチュラル(NN);p:シンクの位置:右シンクタイプ:左シンクタイプ;p:ガス種:都市ガス13A:都市ガス12A:プロパンガス(LPG);|
	かつ ""を""つをカートに追加する	かつ ""を""つをカートに追加する	もし "カート"のページを見ています		ならば ""を見るべき	かつ ""を見るべき	かつ ""を見るべき
	かつ リスト"カート","カート商品"の"タイトル"に""を見るべき  例:
	| code    | title | qty | code2   | title2 | qty2 |
	| TEST001 | 商品1 | 1   | TEST002 | 商品2 | 1    |
	| TEST002 | 商品2 | 1   | TEST003 | 商品3 | 2    |	| TEST003 | 商品3 | 3   | TEST001 | 商品1 | 1    |  

and you should be able to see "title2" in "cart", "cart item"'s, Then in my cucumber steps I have the following

 

Then /^リスト"([^\"]*)","([^\"]*)"の"([^\"]*)"に"([^\"]*)"を見るべき$/ do |parent_object, object, column, item|
  object=word(object)
  column=word(column)
  parent_object=word(parent_object)
  get = JSON.parse(response_body)
  get[parent_object][object].collect { |p| p[column]}.should contain(item)
end

 

 

 

 

The first couple of lines are to grab Japanese translations then the rest gets the JSON data and tells me if it includes what I am hoping for in the right place in the object.

 

Tags:

Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.

Recent Blog Posts