
attractive, usable
web applications
Richard's blog, some of my experiences with web design
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 |
This is my rather funky test which works in Japanese (ignore the fact that it needs to by DRY'ed up a little).
The last line means
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.
Cucumber, Ruby on Rails, TestingCreated on Monday, November 2, 2009 - 14:27
Recent Blog Posts
- Doctrine autoload problem in version 1.2.1 Mon, 02/15/2010 - 14:05
- Get an array of associations for a rails model Tue, 01/26/2010 - 10:09
- Programmers are most effective when they avoid writing code Tue, 01/19/2010 - 23:39
- Making multi website systems Tue, 01/26/2010 - 10:10
- A bash script to download your remote MySQL schema and load it locally Thu, 11/26/2009 - 11:43
Tags
Stripes Java Templating PHP Databases Software Documentation Ruby Agile Practices Business in Japan Web Dev Ruby on Rails Web AdminMonthly archive
- October 2009 (12)
- November 2009 (4)
- December 2009 (1)
- January 2010 (1)
- February 2010 (1)
Write a comment