attractive, usable
web applications


Richard's blog, some of my experiences with web design

Web Apps

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>"を見るべき
    かつ "<code2>"を見るべき
    かつ "<title2>"を見るべき
    かつ リスト"カート","カート商品"の"タイトル"に"<title2>"を見るべき

    例:
      | code    | title | qty | code2   | title2 | qty2 |
      | TEST001 | 商品1 | 1   | TEST002 | 商品2 | 1    |
      | TEST002 | 商品2 | 1   | TEST003 | 商品3 | 2    |
      | TEST003 | 商品3 | 3   | TEST001 | 商品1 | 1    |
</pre>
<p>This is my rather funky test which works in Japanese (ignore the fact that it needs to by DRY'ed up a little).</p>
<p>The last line means</p>
<pre>and you should be able to see "title2" in "cart", "cart item"'s </pre>
<p>Then in my cucumber steps I have the following</p>
<pre>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</pre>
<p>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.</p>        
          <label class="views-label-tid">
        Tags:
      </label>
        <a href="/category/tags/cucumber">Cucumber</a>, <a href="/category/tags/ruby-rails">Ruby on Rails</a>, <a href="/category/tags/testing">Testing</a>        
        <h4>Created on Monday, November 2, 2009 - 14:27</h4>        <div class="comment-section"><p class="comment-link">Write a comment</p><form action="/comment/reply/48"  accept-charset="UTF-8" method="post" id="comment-form">
<div><div class="form-item" id="edit-name-wrapper">
 <label for="edit-name">Your name: </label>
 <input type="text" maxlength="60" name="name" id="edit-name" size="30" value="Anonymous" class="form-text" />
</div>
<div class="form-item" id="edit-mail-wrapper">
 <label for="edit-mail">E-mail: </label>
 <input type="text" maxlength="64" name="mail" id="edit-mail" size="30" value="" class="form-text" />
 <div class="description">The content of this field is kept private and will not be shown publicly.</div>
</div>
<div class="form-item" id="edit-homepage-wrapper">
 <label for="edit-homepage">Homepage: </label>
 <input type="text" maxlength="255" name="homepage" id="edit-homepage" size="30" value="" class="form-text" />
</div>
<div class="form-item" id="edit-subject-wrapper">
 <label for="edit-subject">Subject: </label>
 <input type="text" maxlength="64" name="subject" id="edit-subject" size="60" value="" class="form-text" />
</div>
<div class="form-item" id="edit-comment-wrapper">
 <label for="edit-comment">Comment: <span class="form-required" title="This field is required.">*</span></label>
 <textarea cols="60" rows="15" name="comment" id="edit-comment"  class="form-textarea resizable required"></textarea>
</div>
<ul class="tips"><li>Web page addresses and e-mail addresses turn into links automatically.</li><li>Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd></li><li>Lines and paragraphs break automatically.</li></ul><p><a href="/filter/tips">More information about formatting options</a></p><input type="hidden" name="form_build_id" id="form-016634bd761d452ed418cf59d53cc61b" value="form-016634bd761d452ed418cf59d53cc61b"  />
<input type="hidden" name="form_id" id="edit-comment-form" value="comment_form"  />
<fieldset class="captcha"><legend>CAPTCHA</legend><div class="description">Are you human?</div><input type="hidden" name="captcha_sid" id="edit-captcha-sid" value="72955"  />
<input type="hidden" name="captcha_response" id="edit-captcha-response" value="reCAPTCHA"  />
<div class="form-item">
 <script type="text/javascript" src="http://api.recaptcha.net/challenge?k=6Le-ogkAAAAAAHU1xTGW1YZlpNFJH6vgW0JDu_XH"></script>

	<noscript>
  		<iframe src="http://api.recaptcha.net/noscript?k=6Le-ogkAAAAAAHU1xTGW1YZlpNFJH6vgW0JDu_XH" height="300" width="500" frameborder="0"></iframe><br/>
  		<textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>
  		<input type="hidden" name="recaptcha_response_field" value="manual_challenge"/>
	</noscript>
</div>
</fieldset>
<input type="submit" name="op" id="edit-submit" value="Save"  class="form-submit" />
<input type="submit" name="op" id="edit-preview" value="Preview"  class="form-submit" />

</div></form>
</div>  
  </div>
  
  
  
  
  
  <br clear="all" style="height:0px" />
  <h2>Recent Blog Posts</h2>

       
  
  
        <ul class="recent-blogs">
      <li>      
        <a href="/blog/Doctrine-autoload-problem-in-version-1.2.1">Doctrine autoload problem in version 1.2.1</a>        
        Mon, 02/15/2010 - 14:05  
	</li>
      <li>      
        <a href="/blog/Get-an-array-of-associations-for-a-rails-model-">Get an array of associations for a rails model </a>        
        Tue, 01/26/2010 - 10:09  
	</li>
      <li>      
        <a href="/blog/Programmers-are-most-effective-when-they-avoid-writing-code">Programmers are most effective when they avoid writing code</a>        
        Tue, 01/19/2010 - 23:39  
	</li>
      <li>      
        <a href="/blog/Making-multi-website-systems">Making multi website systems</a>        
        Tue, 01/26/2010 - 10:10  
	</li>
      <li>      
        <a href="/blog/A-bash-script-to-download-your-remote-MySQL-schema-and-load-it-locally">A bash script to download your remote MySQL schema and load it locally</a>        
        Thu, 11/26/2009 - 11:43  
	</li>
  </ul>  
  
  
  
  
  <br clear="all" style="height:0px" />	</div>
		<div id="left-foot"></div>
		</div>

		<div id="right-box">
		<div id="right-body">
		
  <h2>Tags</h2>

    <a href="/category/tags/stripes" class="tagadelic level4" rel="tag">Stripes</a> 
<a href="/category/tags/java" class="tagadelic level4" rel="tag">Java</a> 
<a href="/category/tags/templating" class="tagadelic level1" rel="tag">Templating</a> 
<a href="/category/tags/php" class="tagadelic level4" rel="tag">PHP</a> 
<a href="/category/tags/databases" class="tagadelic level6" rel="tag">Databases</a> 
<a href="/category/tags/software-documentation" class="tagadelic level1" rel="tag">Software Documentation</a> 
<a href="/category/tags/ruby" class="tagadelic level6" rel="tag">Ruby</a> 
<a href="/category/tags/agile-practices" class="tagadelic level4" rel="tag">Agile Practices</a> 
<a href="/category/tags/business-japan" class="tagadelic level4" rel="tag">Business in Japan</a> 
<a href="/category/tags/web-dev" class="tagadelic level9" rel="tag">Web Dev</a> 
<a href="/category/tags/ruby-rails" class="tagadelic level10" rel="tag">Ruby on Rails</a> 
<a href="/category/tags/web-admin" class="tagadelic level9" rel="tag">Web Admin</a> 
<div class='more-link'><a href="/tagadelic/chunk/2">more tags</a></div>
  <h2>Monthly archive</h2>

       
  
  
        <div class="item-list">
  <ul class="views-summary">
      <li><a href="/archive/200910">October 2009</a>
              (12)
          </li>
      <li><a href="/archive/200911">November 2009</a>
              (4)
          </li>
      <li><a href="/archive/200912">December 2009</a>
              (1)
          </li>
      <li><a href="/archive/201001">January 2010</a>
              (1)
          </li>
      <li><a href="/archive/201002">February 2010</a>
              (1)
          </li>
    </ul>
</div>
  
  
  
  
  
  <br clear="all" style="height:0px" />	</div>
		<div id="right-foot"></div>
		</div>


		<br clear="all" />
		<div id="footer">
				Website designed and created by Richard McIntyre using Drupal
		</div>
		</div>
		<script type="text/javascript" src="/sites/default/files/js/js_e75be3aceb85b4a5da3e1d3978032d43.js"></script>
<script type="text/javascript">
<!--//--><![CDATA[//><!--
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
//--><!]]>
</script>
<script type="text/javascript">
<!--//--><![CDATA[//><!--
try{var pageTracker = _gat._getTracker("UA-2266211-2");pageTracker._trackPageview();} catch(err) {}
//--><!]]>
</script>
	</body>
</html>