
front and back-end web development, Leeds, UK
Richard's Blog - Design, coding and life in Japan
Testing output JSON data from a RESTful API with Cucumber
Submitted by Richard on Mon, 11/02/2009 - 14:27Some 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:
Recent Blog Posts
- Testing controllers in Lithium 2nd Feb 12, 18:19
- Practical Internationalization in Lithium 31st Dec 11, 02:06
- Using OAuth in Lithium 30th Dec 11, 23:47
- How to add your own Tokens from CCK fields in Druapl 7 17th Jun 11, 04:49
- Weaving Lithium #li3 into a legacy PHP application incrementally 5th Oct 10, 11:54
Post new comment