<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>validates_presence_of :purpose : Tag javascript, everything about javascript</title>
    <link>http://lenaherrmann.net</link>
    <language>en_US</language>
    <ttl>40</ttl>
    <description>Lena Herrmann</description>
    <item>
      <title>JavaScript syntax highlighting in the LaTeX listings package</title>
      <description>&lt;p&gt;I&amp;#8217;m currently writing my thesis, using &lt;a href="http://www.latex-project.org/"&gt;LaTeX&lt;/a&gt;. As the programming part of my thesis was mostly done in Javascript, I have to include some bits of Javascript source code in the LaTeX document. For this I&amp;#8217;m using the &lt;a href="http://en.wikibooks.org/wiki/LaTeX/Packages/Listings"&gt;listings package&lt;/a&gt;. With this package you can &amp;#8220;include the source code of any programming language within your document&amp;#8221;. Unfortunately Javascript is not in the list of supported programming languages. (In return, the list contains languages like Oberon-2, Promela or RSL, which are probably being used way more often than Javascript &amp;#8230; er.)&lt;/p&gt;


&lt;p&gt;Luckily &lt;a href="http://twitter.com/awendt"&gt;@awendt&lt;/a&gt; pointed me to these links: &lt;a href="http://vis4.net/blog/2009/08/javascript-quelltexte-in-latex/"&gt;Javascript Quelltexte in LaTeX&lt;/a&gt; and &lt;a href="http://mail.asta.uni-potsdam.de/pipermail/uplug/2006-June/003595.html"&gt;a thread on a mailinglist&lt;/a&gt;. I changed those examples only a bit, I&amp;#8217;m posting my result here mainly in the hope it will produce a Google hit for &amp;#8220;javascript latex source code&amp;#8221; :-)&lt;/p&gt;


&lt;p&gt;Put this at the beginning of your document. It will define support for the Javascript language. I included the keywords that show up in my code most frequently.

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="er"&gt;\&lt;/span&gt;usepackage{listings}
&lt;span class="er"&gt;\&lt;/span&gt;usepackage{color}
&lt;span class="er"&gt;\&lt;/span&gt;definecolor{lightgray}{rgb}{.&lt;span class="i"&gt;9&lt;/span&gt;,.&lt;span class="i"&gt;9&lt;/span&gt;,.&lt;span class="i"&gt;9&lt;/span&gt;}
&lt;span class="er"&gt;\&lt;/span&gt;definecolor{darkgray}{rgb}{.&lt;span class="i"&gt;4&lt;/span&gt;,.&lt;span class="i"&gt;4&lt;/span&gt;,.&lt;span class="i"&gt;4&lt;/span&gt;}
&lt;span class="er"&gt;\&lt;/span&gt;definecolor{purple}{rgb}{&lt;span class="fl"&gt;0.65&lt;/span&gt;, &lt;span class="fl"&gt;0.12&lt;/span&gt;, &lt;span class="fl"&gt;0.82&lt;/span&gt;}

&lt;span class="er"&gt;\&lt;/span&gt;lstdefinelanguage{&lt;span class="co"&gt;JavaScript&lt;/span&gt;}{
  keywords={typeof, new, &lt;span class="pc"&gt;true&lt;/span&gt;, &lt;span class="pc"&gt;false&lt;/span&gt;, catch, function, &lt;span class="r"&gt;return&lt;/span&gt;, null, catch, switch, var, &lt;span class="r"&gt;if&lt;/span&gt;, &lt;span class="r"&gt;in&lt;/span&gt;, &lt;span class="r"&gt;while&lt;/span&gt;, &lt;span class="r"&gt;do&lt;/span&gt;, &lt;span class="r"&gt;else&lt;/span&gt;, &lt;span class="r"&gt;case&lt;/span&gt;, &lt;span class="r"&gt;break&lt;/span&gt;},
  keywordstyle=&lt;span class="er"&gt;\&lt;/span&gt;color{blue}&lt;span class="er"&gt;\&lt;/span&gt;bfseries,
  ndkeywords={&lt;span class="r"&gt;class&lt;/span&gt;, export, boolean, throw, implements, import, this},
  ndkeywordstyle=&lt;span class="er"&gt;\&lt;/span&gt;color{darkgray}&lt;span class="er"&gt;\&lt;/span&gt;bfseries,
  identifierstyle=&lt;span class="er"&gt;\&lt;/span&gt;color{black},
  sensitive=&lt;span class="pc"&gt;false&lt;/span&gt;,
  comment=[l]{&lt;span class="rx"&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;/span&gt;},
  morecomment=[s]{&lt;span class="rx"&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;span class="k"&gt;*}{*&lt;/span&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;/span&gt;},
  commentstyle=&lt;span class="er"&gt;\&lt;/span&gt;color{purple}&lt;span class="er"&gt;\&lt;/span&gt;ttfamily,
  stringstyle=&lt;span class="er"&gt;\&lt;/span&gt;color{red}&lt;span class="er"&gt;\&lt;/span&gt;ttfamily,
  morestring=[b]&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;,
  morestring=[b]&amp;quot;
}&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;Here you define the style for the newly defined language. This also belongs in the preamble of your LaTeX file.
  
&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="er"&gt;\&lt;/span&gt;lstset{
   language=&lt;span class="co"&gt;JavaScript&lt;/span&gt;,
   backgroundcolor=&lt;span class="er"&gt;\&lt;/span&gt;color{lightgray},
   extendedchars=&lt;span class="pc"&gt;true&lt;/span&gt;,
   basicstyle=&lt;span class="er"&gt;\&lt;/span&gt;footnotesize&lt;span class="er"&gt;\&lt;/span&gt;ttfamily,
   showstringspaces=&lt;span class="pc"&gt;false&lt;/span&gt;,
   showspaces=&lt;span class="pc"&gt;false&lt;/span&gt;,
   numbers=left,
   numberstyle=&lt;span class="er"&gt;\&lt;/span&gt;footnotesize,
   numbersep=&lt;span class="i"&gt;9&lt;/span&gt;pt,
   tabsize=&lt;span class="i"&gt;2&lt;/span&gt;,
   breaklines=&lt;span class="pc"&gt;true&lt;/span&gt;,
   showtabs=&lt;span class="pc"&gt;false&lt;/span&gt;,
   captionpos=b
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;The colours are just an example, read how to define more colours &lt;a href="http://en.wikibooks.org/wiki/LaTeX/Colors#Defining_new_colors"&gt;in the wikibook&lt;/a&gt;.&lt;/p&gt;


&lt;p&gt;Now you can insert code like this. The example is nonsense, I tried to include as many keywords as possible. The &lt;i&gt;medskip&lt;/i&gt; command before the listing inserts a linebreak, a not very nice way to have some distance between the text and the listing.

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="er"&gt;\&lt;/span&gt;medskip
&lt;span class="er"&gt;\&lt;/span&gt;begin{lstlisting}[caption=My Javascript Example]
Name.prototype = {
  &lt;span class="ke"&gt;methodName&lt;/span&gt;: &lt;span class="kw"&gt;function&lt;/span&gt;(params){
    &lt;span class="kw"&gt;var&lt;/span&gt; doubleQuoteString = &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;some text&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;;
    &lt;span class="kw"&gt;var&lt;/span&gt; singleQuoteString = &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;some more text&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;;
    &lt;span class="c"&gt;// this is a comment&lt;/span&gt;
    &lt;span class="kw"&gt;if&lt;/span&gt;(&lt;span class="lv"&gt;this&lt;/span&gt;.confirmed != &lt;span class="kw"&gt;null&lt;/span&gt; &amp;amp;&amp;amp; &lt;span class="kw"&gt;typeof&lt;/span&gt;(&lt;span class="lv"&gt;this&lt;/span&gt;.confirmed) == Boolean &amp;amp;&amp;amp; &lt;span class="lv"&gt;this&lt;/span&gt;.confirmed == &lt;span class="kw"&gt;true&lt;/span&gt;){
      document.createElement(&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;h3&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;);
      &lt;span class="pd"&gt;$&lt;/span&gt;(&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;#system&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;).append(&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;This looks great&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;);
      &lt;span class="kw"&gt;return&lt;/span&gt; &lt;span class="kw"&gt;false&lt;/span&gt;;
    } &lt;span class="kw"&gt;else&lt;/span&gt; {
      &lt;span class="kw"&gt;throw&lt;/span&gt; &lt;span class="kw"&gt;new&lt;/span&gt; Error;
    }
  }
}
&lt;span class="er"&gt;\&lt;/span&gt;end{lstlisting}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;The result looks like this:&lt;/p&gt;


&lt;p&gt;&lt;img width="532"  src="/files/latex.png" alt="Javascript source code in LaTeX"/&gt;&lt;/p&gt;


&lt;p&gt;Update: &lt;a href="http://twitter.com/perkee"&gt;@perkee&lt;/a&gt; pointed out you can also use the &lt;a href="http://code.google.com/p/minted/"&gt;minted package&lt;/a&gt; for syntax highlighting.&lt;/p&gt; 
</description>
      <pubDate>Thu, 20 May 2010 11:20:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:53c2e2e5-5f54-4169-9b9a-d48e347b4cf3</guid>
      <comments>http://lenaherrmann.net/2010/05/20/javascript-syntax-highlighting-in-the-latex-listings-package#comments</comments>
      <category>javascript</category>
      <category>latex</category>
      <trackback:ping>http://lenaherrmann.net/trackbacks?article_id=16</trackback:ping>
      <link>http://lenaherrmann.net/2010/05/20/javascript-syntax-highlighting-in-the-latex-listings-package</link>
    </item>
    <item>
      <title>Security in CouchDB: changing the authentication db</title>
      <description>&lt;p&gt;I recently &lt;a href="http://lenaherrmann.net/2010/04/28/writing-a-testsuite-for-the-couchdb-api"&gt;wrote a testsuite for CouchDB&amp;#8217;s Javascript HTTP API&lt;/a&gt;. Among the things I tested were the security methods. That&amp;#8217;s those that deal with authentication - like signup, login, logout and so on. The challenge was to make sure my tests wouldn&amp;#8217;t interfere with the CouchDB setup of the person running the tests.&lt;/p&gt;


&lt;h3 style="color:#949490;margin-left:6px;"&gt;Background&lt;/h3&gt;


&lt;p&gt;CouchDB stores the information about the database users in a special database, the authentication_db. The default authentication_db is called &amp;#8220;_users&amp;#8221;. So this is where your user data is saved when you sign up without taking care about the details, e.g. when you use &lt;a href="http://github.com/lenalena/couchdb/blob/testsuite/share/www/script/jquery.couch.js#L112-119"&gt;the signup method provided by the JQuery HTTP API&lt;/a&gt;. Read more about the ways to interact with the authentication_db in &lt;a href="http://github.com/lenalena/couchdb-http-api-docs/blob/master/couch_js_security_methods.markdown"&gt;the HTTP API documentation&lt;/a&gt; (&lt;a href="http://github.com/lenalena/couchdb-http-api-docs/blob/master/jquery_couch_js_security_methods.markdown"&gt;JQuery methods&lt;/a&gt;), and read more about general CouchDB security &lt;a href="http://wiki.apache.org/couchdb/Security_Features_Overview"&gt;in the CouchDB Wiki&lt;/a&gt; or in &lt;a href="http://books.couchdb.org/relax/reference/security"&gt;The Definitive Guide&lt;/a&gt;.&lt;/p&gt;


&lt;h3 style="color:#949490;margin-left:6px;"&gt;How to set the authentication_db&lt;/h3&gt;


&lt;p&gt;When you e.g. create database users from within your tests, how can you make sure they don&amp;#8217;t mess up the actual authentication_db, the one the unsuspecting user might use in production? Of course you have to create a custom authentication_db for storing the user information, and make CouchDB use that one. And after your tests are finished, you want CouchDB to use the original authentication_db again, so that only your testsuite uses the custom users database.&lt;/p&gt;  


&lt;p&gt;So there is one function you call right at the beginning of the part of the testsuite that&amp;#8217;s concerned with security features:

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="kw"&gt;function&lt;/span&gt; useTestUserDb(){
  test_users_db = &lt;span class="kw"&gt;new&lt;/span&gt; CouchDB(&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;test_users_db&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;);
  &lt;span class="kw"&gt;var&lt;/span&gt; xhr = CouchDB.request(&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;PUT&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;/_config/couch_httpd_auth/authentication_db&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, {
    &lt;span class="ke"&gt;body&lt;/span&gt;: JSON.stringify(&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;test_users_db&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;)
  });
  &lt;span class="kw"&gt;if&lt;/span&gt;(&lt;span class="kw"&gt;typeof&lt;/span&gt;(old_auth_db) == &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;undefined&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;){
    old_auth_db = xhr.responseText.replace(&lt;span class="rx"&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;span class="ch"&gt;\n&lt;/span&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;/span&gt;,&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;).replace(&lt;span class="rx"&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;span class="k"&gt;&amp;quot;&lt;/span&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;span class="mod"&gt;g&lt;/span&gt;&lt;/span&gt;,&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;);
  }
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;We create a custom authentication_db (&amp;#8220;test_users_db&amp;#8221;) and write its name into the CouchDB configuration. If we don&amp;#8217;t have the name of the old authentication_db stored yet (the one that&amp;#8217;s called &amp;#8220;_users&amp;#8221; per default, but might be called differently), we strip this name from special characters and store it in a variable, so we can restore it later. This is the function we call at the very end of the testsuite:

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="kw"&gt;function&lt;/span&gt; useOldUserDb(){
  CouchDB.request(&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;PUT&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;/_config/couch_httpd_auth/authentication_db&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, {
    &lt;span class="ke"&gt;body&lt;/span&gt;: JSON.stringify(old_auth_db)
  });
  test_users_db.deleteDb();
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;In the last line, we delete our custom authentication_db. Everything is back to normal, there is no trace of our tests left. You can have a look at &lt;a href="http://github.com/lenalena/couchdb/blob/testsuite/share/www/spec/couch_js_class_methods_spec.js"&gt;how it looks like in production.&lt;/a&gt;&lt;/p&gt;


&lt;h3 style="color:#949490;margin-left:6px;"&gt;How to work with the authentication_db&lt;/h3&gt;


&lt;p&gt;You can create a new user like this:

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="kw"&gt;var&lt;/span&gt; userDoc = CouchDB.prepareUserDoc({&lt;span class="ke"&gt;name&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;Gaius Baltar&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="ke"&gt;roles&lt;/span&gt;: [&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;president&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;]}, &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;secretpassword&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;);
test_users_db.save(userDoc);&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;Then you are logged in and can do whatever you want. About working with users and roles, read the &lt;a href="http://books.couchdb.org/relax/reference/security"&gt;chapter in The Definitive Guide&lt;/a&gt;. When you&amp;#8217;re done, delete the userDoc from the authentication_db, as the authentication_db has to be empty before it can be deleted:
  
&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;test_users_db.deleteDoc({&lt;span class="ke"&gt;_id&lt;/span&gt; : userDoc.id, &lt;span class="ke"&gt;_rev&lt;/span&gt; : userDoc.rev})&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;
</description>
      <pubDate>Thu, 29 Apr 2010 10:40:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:f3260d78-d5a7-4750-9429-b21c2cef7f0a</guid>
      <comments>http://lenaherrmann.net/2010/04/29/security-in-couchdb-changing-the-authentication-db#comments</comments>
      <category>couchdb</category>
      <category>javascript</category>
      <category>authentication</category>
      <category>stubbing</category>
      <trackback:ping>http://lenaherrmann.net/trackbacks?article_id=14</trackback:ping>
      <link>http://lenaherrmann.net/2010/04/29/security-in-couchdb-changing-the-authentication-db</link>
    </item>
    <item>
      <title>JSpec: Slides of my talk at Berlin JS Usergroup</title>
      <description>&lt;p&gt;Last thursday I gave a talk at the monthly meeting of the &lt;a href="http://twitter.com/berlinjs"&gt;Berlin JS Usergroup&lt;/a&gt;. I was talking about &lt;a href="http://github.com/visionmedia/jspec"&gt;JSpec&lt;/a&gt;, a Javascript Unit Testing framework. Thanks to all who were there to listen and ask questions!&lt;/p&gt;


&lt;p&gt;Here are &lt;a href="http://www.slideshare.net/kilaulena/jspec-a-javascript-unit-testing-framework"&gt;the slides&lt;/a&gt;. When you have a look at them, make sure to also read the notes (next to the comments), so the slides make sense to you.
&lt;/p&gt;


&lt;p&gt;To find more information about JSpec, check out &lt;a href="http://github.com/visionmedia/jspec"&gt;the README on github&lt;/a&gt;, or read the &lt;a href="http://lenaherrmann.net/2010/01/04/jspec-javascript-unit-testing-how-it-should-be"&gt;blog post I wrote about it recently&lt;/a&gt;.&lt;p&gt;

&lt;p&gt;Thanks also to &lt;a href="http://twitter.com/gisikw"&gt;@gisikw&lt;/a&gt; and &lt;a href="http://twitter.com/tjholowaychuk"&gt;@tjholowaychuk&lt;/a&gt; for &lt;a href="http://slides.kevingisi.com/TestYourJavaScript/"&gt;this talk&lt;/a&gt; from which I borrowed a few grammar examples :)&lt;/p&gt;
</description>
      <pubDate>Mon, 22 Mar 2010 12:52:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:8e64ebfd-22ee-4b7b-885d-96a488801c87</guid>
      <comments>http://lenaherrmann.net/2010/03/22/jspec-slides-of-my-talk-at-berlin-js-usergroup#comments</comments>
      <category>jspec</category>
      <category>talk</category>
      <category>slides</category>
      <category>testing</category>
      <category>javascript</category>
      <category>berlinjs</category>
      <category>usergroup</category>
      <trackback:ping>http://lenaherrmann.net/trackbacks?article_id=12</trackback:ping>
      <link>http://lenaherrmann.net/2010/03/22/jspec-slides-of-my-talk-at-berlin-js-usergroup</link>
    </item>
    <item>
      <title>Refactoring asynchronous code</title>
      <description>&lt;p&gt;How to refactor a long chunk of asynchronous code is one thing I learned during my Javascript &amp; CouchDB project. It&amp;#8217;s not a difficult thing, but I thought it was before I figured out how to do it, so I guess it might be interesting for Javascript newbies.&lt;/p&gt;


&lt;h3 style="color:#949490;margin-left:6px;"&gt;Asynchronous code&lt;/h3&gt;


&lt;p&gt;In the first couple of weeks I struggled with this new style of coding before I got the hang of it. If you&amp;#8217;re fairly new to Javascript, you&amp;#8217;re probably used to just assign a variable in one line and use it in the next line. In the asynchronous world you query for a value and do stuff with it &lt;i&gt;in the callback function&lt;/i&gt; of the query.&lt;/p&gt;


&lt;p&gt;To illustrate - &amp;#8220;normal&amp;#8221; code looks like this:

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="kw"&gt;var&lt;/span&gt; result = db.query(&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;select * from T&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;);
&lt;span class="c"&gt;// use result&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;Although writing web applications just screams for doing it like this instead:
  
&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;
db.query(&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;select..&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="kw"&gt;function&lt;/span&gt; (result) {
&lt;span class="c"&gt;// use result&lt;/span&gt;
});&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p style="font-size:0.8em"&gt;(Examples stolen from &lt;a href="http://nodejs.org/"&gt;Ryan Dahl&amp;#8217;s Presentation about Node.js&lt;/a&gt;.)&lt;/p&gt;


&lt;h3 style="color:#949490;margin-left:6px;"&gt;The Problem&lt;/h3&gt;


&lt;p&gt;&lt;img style="float:left;margin-right:10px;" width="250"  src="http://lenaherrmann.net/files/async-bad.png" alt="Bad asynchronous code" /&gt;&lt;/p&gt;


&lt;p&gt;I have a method that checks if I have to display a notification message. For this it makes a lot of requests to the database, the received data has to be compared with other data, there&amp;#8217;s lots of if/else clauses to consider all kinds of conditions and edge cases. It started simple, but then more and more conditions came in, unless I had the &lt;a href="http://geekandpoke.typepad.com/.a/6a00d8341d3df553ef0128766398f7970c-pi"&gt;much feared diagonal closing bracket line&lt;/a&gt; on my screen. &lt;/p&gt;


&lt;p&gt;You don&amp;#8217;t need to know the details, just look at the picture and you&amp;#8217;ll get the impression.&lt;/p&gt;


&lt;h3 style="color:#949490;margin-left:6px;clear:left"&gt;Refactoring&lt;/h3&gt;


&lt;p&gt;By way of illustration I made up a much shorter example:

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;
checkForConflicts: &lt;span class="kw"&gt;function&lt;/span&gt;(){
  &lt;span class="c"&gt;//set arguments&lt;/span&gt;
  openDoc(id, &lt;span class="kw"&gt;function&lt;/span&gt;(result){
    &lt;span class="c"&gt;// do stuff with arguments&lt;/span&gt;
    &lt;span class="kw"&gt;if&lt;/span&gt;(result.bar == whatever){
      &lt;span class="c"&gt;// do more stuff with result&lt;/span&gt;
      openAnotherDoc(id2, &lt;span class="kw"&gt;function&lt;/span&gt;(result2){
        &lt;span class="kw"&gt;if&lt;/span&gt;(result2.baz == &lt;span class="kw"&gt;true&lt;/span&gt;){
          &lt;span class="c"&gt;// show notification message with result2&lt;/span&gt;
        }
      });
    }
  });
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;It is time to refactor after you are sure this actually is the way to go and the code basically works (&amp;#8220;make it work, then make it pretty&amp;#8221;). First you group the code into methods: You split the lines by what they do, and then you name each section. I think around 8 LOC is a good length for a method. &lt;/p&gt;


&lt;p&gt;If this was synchronous code, you could just call these methods after each other. But this doesn&amp;#8217;t work here, because &lt;i&gt;result&lt;/i&gt; and &lt;i&gt;result2&lt;/i&gt; are not available outside the function that retrieves them. &lt;/p&gt;


&lt;p&gt;My first approach was to call the functions from within each other. This works - but to see what &lt;i&gt;checkForConflicts&lt;/i&gt; does at its very heart, you have to scroll down through all the methods - not much gained:

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;checkForConflicts: &lt;span class="kw"&gt;function&lt;/span&gt;(){
  &lt;span class="c"&gt;//set arguments&lt;/span&gt;
  doSomething(&lt;span class="lv"&gt;arguments&lt;/span&gt;);
}

doSomething(&lt;span class="lv"&gt;arguments&lt;/span&gt;){
  openDoc(id, &lt;span class="kw"&gt;function&lt;/span&gt;(result){
    &lt;span class="c"&gt;// do stuff with arguments&lt;/span&gt;
    &lt;span class="kw"&gt;if&lt;/span&gt;(result.bar = whatever){
      doSomethingElse(result);
    }
  });
}

doSomethingElse(result){
  &lt;span class="c"&gt;// do more stuff with result&lt;/span&gt;
  openAnotherDoc(id2, &lt;span class="kw"&gt;function&lt;/span&gt;(result2){
    &lt;span class="kw"&gt;if&lt;/span&gt;(result2.baz == &lt;span class="kw"&gt;true&lt;/span&gt;){
      showMessage(result2);
    }
  });
}

showMessage = &lt;span class="kw"&gt;function&lt;/span&gt;(result2){
  &lt;span class="c"&gt;// show notification message with result2&lt;/span&gt;
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;The way to do it is to use &lt;i&gt;callbacks&lt;/i&gt;. Each method gets an anonymous function as (additional) argument: in the method declaration this function has the name &lt;i&gt;callback&lt;/i&gt;. This callback is called if all the conditions within the method are met. The callback gets its arguments from within the method that calls it. You have to specify these arguments again in &lt;i&gt;checkForConflicts&lt;/i&gt; to have a valid function definition.

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;checkForConflicts: &lt;span class="kw"&gt;function&lt;/span&gt;(){
  &lt;span class="c"&gt;//set arguments&lt;/span&gt;
  doSomething(&lt;span class="lv"&gt;arguments&lt;/span&gt;, &lt;span class="kw"&gt;function&lt;/span&gt;(result){
    doSomethingElse(result, &lt;span class="kw"&gt;function&lt;/span&gt;(result2){
      showMessage(result2); 
    });
  });
}

doSomething(&lt;span class="lv"&gt;arguments&lt;/span&gt;, callback){
  openDoc(id, &lt;span class="kw"&gt;function&lt;/span&gt;(result){
    &lt;span class="c"&gt;// do stuff with arguments&lt;/span&gt;
    &lt;span class="kw"&gt;if&lt;/span&gt;(result.bar = whatever){
      callback(result);
    }
  });
}

doSomethingElse(result, callback){
  &lt;span class="c"&gt;// do more stuff with result&lt;/span&gt;
  openAnotherDoc(id2, &lt;span class="kw"&gt;function&lt;/span&gt;(result2){
    &lt;span class="kw"&gt;if&lt;/span&gt;(result2.baz == &lt;span class="kw"&gt;true&lt;/span&gt;){
      callback(result2);
    }
  });
}

showMessage = &lt;span class="kw"&gt;function&lt;/span&gt;(result2){
  &lt;span class="c"&gt;// show notification message with result2&lt;/span&gt;
}&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;When you look at &lt;i&gt;checkForConflicts&lt;/i&gt;, you now immediately see what it does, without being bothered by the details. As a side effect, deciding which code needs access to which arguments helps you to understand your code better. In my case I was able to optimize the use of passed variables a lot.&lt;/p&gt;


&lt;p&gt;&lt;img style="float:left;margin-right:10px;" width="250"  src="http://lenaherrmann.net/files/async-good.png" alt="Refactored asynchronous code" /&gt;&lt;/p&gt;


&lt;p&gt;You might argue that the code is much longer now. First, longer is not worse if it is more readable. Second, this is only an example - if there was code instead of &lt;i&gt;//do stuff&lt;/i&gt; the few extra lines wouldn&amp;#8217;t carry weight.&lt;/p&gt;


&lt;p&gt;Finally here is a screenshot of my refactored code. All the methods don&amp;#8217;t fit on one screen, but you can see what &lt;i&gt;checkForConflicts&lt;/i&gt; does very quickly.&lt;/p&gt;


&lt;p style="clear:left"/&gt;
</description>
      <pubDate>Mon, 15 Feb 2010 16:17:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:8e9117d9-b37a-467b-99ac-b6f035ed1a9e</guid>
      <comments>http://lenaherrmann.net/2010/02/15/refactoring-asynchronous-code#comments</comments>
      <category>javascript</category>
      <category>refactoring</category>
      <category>ajax</category>
      <trackback:ping>http://lenaherrmann.net/trackbacks?article_id=11</trackback:ping>
      <link>http://lenaherrmann.net/2010/02/15/refactoring-asynchronous-code</link>
    </item>
    <item>
      <title>JSpec - JavaScript Unit testing how it should be</title>
      <description>&lt;p&gt;Selecting a unit test framework for a JavaScript-only application - not a task with an obvious answer! Coming from the Rails world, where most people work with either TestUnit or RSpec, I was a bit lost when I started researching. &lt;a href="http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#JavaScript"&gt;This table on wikipedia&lt;/a&gt; was quite helpful for a first overview. But: JsUnit, JSTest, jsUnitTest, jsUnity - these are actually different frameworks, and there&amp;#8217;s even many more &amp;#8230;&lt;/p&gt;


&lt;p&gt;I narrowed the selection down to those being currently developed, or are in use in &amp;#8220;big&amp;#8221; projects. &lt;a href="http://www.jsunit.net/"&gt;JsUnit&lt;/a&gt; seems to be one of the major players, but I found almost no documentation, and the code base hasn&amp;#8217;t changed a lot in the last years. That&amp;#8217;s not a negative thing in itself, but having an active community around is a good thing. &lt;a href="http://docs.jquery.com/QUnit"&gt;QUnit&lt;/a&gt; is used for testing JQuery, it looked like you could work with it as well, but its syntax and setup didn&amp;#8217;t make me go &amp;#8220;wow&amp;#8221; exactly. Same goes for &lt;a href="http://code.google.com/p/jqunit/"&gt;JQUnit&lt;/a&gt;, about which I had read good stuff in several blog posts. It&amp;#8217;s also what Sammy.js is tested with.&lt;/p&gt;


&lt;p&gt;I like RSpec, so when I had my first look at &lt;a href="http://github.com/nkallen/screw-unit"&gt;ScrewUnit&lt;/a&gt;, I was delighted to find there&amp;#8217;s BDD syntax for JavaScript too. ScrewUnit is also a part of &lt;a href="http://github.com/relevance/blue-ridge"&gt;Blue Ridge&lt;/a&gt;, which is sadly still only available as a Rails plugin. So, ScrewUnit looked nice.&lt;/p&gt;


&lt;p&gt;And THEN &amp;#8230; I found &lt;a href="http://visionmedia.github.com/jspec/"&gt;JSpec&lt;/a&gt;. I was thrilled! It&amp;#8217;s a JavaScript framework, but it doesn&amp;#8217;t look like JavaScript at all: no curly brackets, no semicolons, just plain domain language. It just looks like RSpec. Here is a random snippet from my test suite, just have a look:

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;describe &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;NoteCollection&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
  before_each
    note3 = &lt;span class="kw"&gt;new&lt;/span&gt; Note({&lt;span class="ke"&gt;id&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;8c8&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="ke"&gt;text&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;three&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;});
    note1 = &lt;span class="kw"&gt;new&lt;/span&gt; Note({&lt;span class="ke"&gt;id&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;ae9&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="ke"&gt;text&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;one&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class="ke"&gt;next_id&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;107&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;});
    note2 = &lt;span class="kw"&gt;new&lt;/span&gt; Note({&lt;span class="ke"&gt;id&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;107&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="ke"&gt;text&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;two&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class="ke"&gt;next_id&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;8c8&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;});
      
    notes = &lt;span class="kw"&gt;new&lt;/span&gt; NoteCollection([note3, note1, note2]);
  end
  
  describe &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;firstNote&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
    it &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;should return a note&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
      notes.firstNote().should.be_an_instance_of Note
    end
    
    it &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;should return the first note&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
      notes.firstNote().id.should.eql note1.id
    end

    it &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;should throw an error if there is more than one note that could be the first note&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
      note4 = &lt;span class="kw"&gt;new&lt;/span&gt; Note({&lt;span class="ke"&gt;id&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;fff&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="ke"&gt;rev&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;2-420&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;});
      notes.notes.push(note4);
      -{notes.firstNote()}.should.throw_error &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;More than one first note found&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
    end  
  end
end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;And that are just 3 out of over 45 core matchers. There are also matchers for JQuery, see the next example, where I put the DOM I wanted to test in a fixture called outline.html. In Rails I don&amp;#8217;t use fixtures, but if you want to test an application where all the markup is generated out of many different sources, it makes sense to test little pieces of the DOM in isolation. In ScrewUnit you have only one file for your all your mocked DOM.

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;describe &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;NoteElement&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
  before_each
    outline = elements(fixture(&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;outline&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;))
    notes = outline.find(&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;li&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;)
  
    parent_note = &lt;span class="kw"&gt;new&lt;/span&gt; NoteElement(&lt;span class="pd"&gt;$&lt;/span&gt;(notes.get(&lt;span class="i"&gt;0&lt;/span&gt;)))
    child_note = &lt;span class="kw"&gt;new&lt;/span&gt; NoteElement(&lt;span class="pd"&gt;$&lt;/span&gt;(notes.get(&lt;span class="i"&gt;1&lt;/span&gt;)))
  end
  
  describe &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;focusNextTextarea&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
    it &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;should focus the first child note if there is one&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
      parent_note.focusNextTextarea();
      parent_note.noteLi().should.not.have_attr(&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;data-focus&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;)
      child_note.noteLi().attr(&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;data-focus&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;).should.eql &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;true&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
    end
  end

  describe &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;insertUpdateNotePointers&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;    
    it &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;should call setNextPointerToNewlyInsertedNote&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
      inserted_note = {&lt;span class="ke"&gt;id&lt;/span&gt;:&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;4&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class="ke"&gt;text&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;inserted!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;};
      parent_note.should.receive(&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;setNextPointerToNewlyInsertedNote&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;).with_args(inserted_note)
      parent_note.insertUpdateNotePointers(inserted_note);
    end
  end
end&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;Note, if you don&amp;#8217;t like the RSpec-like Syntax, you can also skip the JSpec DSL and just use plain Javascript syntax.&lt;/p&gt;


&lt;p&gt;More JSpec goodness: You can either run the tests in the browser, or in the terminal, so you can include it in your CI. In the terminal you can specify the browsers it should be tested with: The browsers will be opened in the background, but you won&amp;#8217;t see your stuff being run like in Selenium. You can mock timers to test asynchronous calls. You can stub methods, mock AJAX requests, specify shared behaviours, hook into JSpec to specify your own matchers or do whatever else, and &amp;#8230; just &lt;a href="http://github.com/visionmedia/jspec/"&gt;see for yourself&lt;/a&gt;.&lt;/p&gt;


&lt;p&gt;Installation is easy too: just add JSpec.js and JSpec.css to your application. If you need them, add the files for jquery, timers or xhr support too. In spec/index.html you include your application files and specify which tests you want to run. Then open it in the browser, wait a few milliseconds, and see the result. If you want to run JSpec tests in the terminal, just install the JSpec Ruby gem. For this you also have to install &lt;a href="http://www.mozilla.org/rhino/"&gt;Rhino&lt;/a&gt; (a Java-based JavaScript interpreter) - when you are on OS X, &lt;a href="http://peter.michaux.ca/articles/installing-rhino-on-os-x"&gt;this should get you started&lt;/a&gt;.&lt;/p&gt;


&lt;p&gt;What I don&amp;#8217;t like about JSpec &amp;#8230; the BlueRidge browser output is much more clear and pretty. I thought really hard about more things but that&amp;#8217;s it.&lt;/p&gt;


&lt;p&gt;Make sure to read the documentation on &lt;a href="http://jspec.info/"&gt;the JSpec website&lt;/a&gt; as well as the &lt;a href="http://github.com/visionmedia/jspec/"&gt;github Readme&lt;/a&gt;. Both ressources look similar, but at the time of writing there were different things on the two pages.&lt;/p&gt;
</description>
      <pubDate>Mon, 04 Jan 2010 12:07:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:c6222c7f-ab27-4e0b-a744-09dde4f35970</guid>
      <comments>http://lenaherrmann.net/2010/01/04/jspec-javascript-unit-testing-how-it-should-be#comments</comments>
      <category>javascript</category>
      <category>jspec</category>
      <category>jquery</category>
      <category>testing</category>
      <trackback:ping>http://lenaherrmann.net/trackbacks?article_id=9</trackback:ping>
      <link>http://lenaherrmann.net/2010/01/04/jspec-javascript-unit-testing-how-it-should-be</link>
    </item>
    <item>
      <title>Bulk deletion of documents in CouchDB</title>
      <description>&lt;p&gt;For the couchapp I'm writing, I recently wanted to do a bulk delete operation with CouchDB, but found very little about it on the web. I don't know if my approach is the best way to go, but well, it works - if you know a better way, I'm happy to learn about it.&lt;/p&gt;

&lt;p&gt;In the file &lt;a href="http://github.com/lenalena/couchdb/blob/trunk/share/www/script/jquery.couch.js"&gt;jquery.couch.js&lt;/a&gt; that comes with CouchDB there are a couple of convenience methods to help you dealing with the database, documents and views. There is a method for deletion too (removeDoc), but it only can handle one document at a time. In the style of bulkSave, I added a bulkRemove method:

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;notextile&gt;&lt;span class="CodeRay"&gt;bulkRemove: &lt;span class="kw"&gt;function&lt;/span&gt;(docs, options){
  docs.docs = &lt;span class="pd"&gt;$&lt;/span&gt;.each(docs.docs, &lt;span class="kw"&gt;function&lt;/span&gt;(i, doc){doc._deleted = &lt;span class="kw"&gt;true&lt;/span&gt;});
  &lt;span class="pd"&gt;$&lt;/span&gt;.extend(options, {&lt;span class="ke"&gt;successStatus&lt;/span&gt;: &lt;span class="i"&gt;201&lt;/span&gt;});
  ajax({
      &lt;span class="ke"&gt;type&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;POST&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;,
      &lt;span class="ke"&gt;contentType&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;application/json&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;,
      &lt;span class="ke"&gt;dataType&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;json&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, 
      &lt;span class="ke"&gt;data&lt;/span&gt;: toJSON(docs),
      &lt;span class="ke"&gt;url&lt;/span&gt;: &lt;span class="lv"&gt;this&lt;/span&gt;.uri + &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;_bulk_docs&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt; + encodeOptions(options)
    },
    options,
    &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;The documents could not be deleted&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;
  );
},&lt;/span&gt;&lt;/notextile&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;In here I add the attribute "_deleted = true" to every document I want to delete, and then I do a bulk update with the documents. That's it.&lt;/p&gt;

&lt;p&gt;This is one possible use case: In the delete action of my controller I first fetch an array with the post and its comments from the couch (how to do this see below), and then I call the bulkRemove action on that array.

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;notextile&gt;&lt;span class="CodeRay"&gt;couchapp.design.view(&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;post_with_comments&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;, {
  &lt;span class="ke"&gt;startkey&lt;/span&gt;: [params[&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;id&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;]],
  &lt;span class="ke"&gt;endkey&lt;/span&gt;: [params[&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;id&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;], {}],
  &lt;span class="ke"&gt;success&lt;/span&gt;: &lt;span class="kw"&gt;function&lt;/span&gt;(json) {
    &lt;span class="kw"&gt;if&lt;/span&gt; (json.rows.length &amp;gt; &lt;span class="i"&gt;0&lt;/span&gt;) { 
      &lt;span class="kw"&gt;var&lt;/span&gt; post_and_comments = json.rows.map(&lt;span class="kw"&gt;function&lt;/span&gt;(row) {&lt;span class="kw"&gt;return&lt;/span&gt; row.value}); 
      couchapp.db.bulkRemove({&lt;span class="ke"&gt;docs&lt;/span&gt;: post_and_comments}, {
        &lt;span class="ke"&gt;success&lt;/span&gt;: &lt;span class="kw"&gt;function&lt;/span&gt;() {
          flash = {&lt;span class="ke"&gt;message&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;Post deleted.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;, &lt;span class="ke"&gt;type&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;notice&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;}
          redirect(&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;#/outlines&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;, flash);
        },
        &lt;span class="ke"&gt;error&lt;/span&gt;: &lt;span class="kw"&gt;function&lt;/span&gt;(response_code, msg) {
          flash = {&lt;span class="ke"&gt;message&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;Error deleting post: &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt; + msg, &lt;span class="ke"&gt;type&lt;/span&gt;: &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;error&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;};
        }
      });
    }      
  }
});&lt;/span&gt;&lt;/notextile&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;Note that the "flash" object I'm using is my own Sammy.js implementation of a Rails-like flash to show a message after a redirect. Maybe I'll share that in another blog post.&lt;/p&gt;

&lt;p&gt;As a bonus, this is the view I'm using to retrieve a post and its comments in one request. I'm following the pattern Christopher Lenz is recommending in his article on &lt;a href="http://www.cmlenz.net/archives/2007/10/couchdb-joins"&gt;CouchDB Joins&lt;/a&gt;.

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;notextile&gt;&lt;span class="CodeRay"&gt;&lt;span class="kw"&gt;function&lt;/span&gt;(doc) {
  &lt;span class="kw"&gt;if&lt;/span&gt; (doc.type == &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;Post&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;) {
    emit([doc._id, &lt;span class="i"&gt;0&lt;/span&gt;], doc);
  } &lt;span class="kw"&gt;else&lt;/span&gt; &lt;span class="kw"&gt;if&lt;/span&gt; (doc.type == &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;Comment&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;) {
    emit([doc.post_id, &lt;span class="i"&gt;1&lt;/span&gt;, Date.parse(doc.created_at)], doc);
  }
}&lt;/span&gt;&lt;/notextile&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;When you call this view with

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;notextile&gt;&lt;span class="CodeRay"&gt;startkey: [params[&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;id&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;]],
&lt;span class="ke"&gt;endkey&lt;/span&gt;: [params[&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;id&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;], {}]&lt;/span&gt;&lt;/notextile&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;you get an array with the post as the first element, and then all the comments that have the post's ID as post_id.&lt;/p&gt;

&lt;p&gt;This can of course also be used for things other than deleting: to show all the comments for a post, I get the array's first element and extract the title and content. Then I remove it from the array with

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;notextile&gt;&lt;span class="CodeRay"&gt;json.rows.splice(&lt;span class="i"&gt;0&lt;/span&gt;,&lt;span class="i"&gt;1&lt;/span&gt;);        &lt;/span&gt;&lt;/notextile&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;You now have an array with only the post's comments and you can display them somehow.&lt;/p&gt;

</description>
      <pubDate>Tue, 22 Dec 2009 17:37:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:150befc7-1e89-47d5-ae0c-47d803336e73</guid>
      <comments>http://lenaherrmann.net/2009/12/22/bulk-deletion-of-documents-in-couchdb#comments</comments>
      <category>couchdb</category>
      <category>javascript</category>
      <category>json</category>
      <trackback:ping>http://lenaherrmann.net/trackbacks?article_id=8</trackback:ping>
      <link>http://lenaherrmann.net/2009/12/22/bulk-deletion-of-documents-in-couchdb</link>
    </item>
    <item>
      <title>Testing key events with Culerity</title>
      <description>&lt;p&gt;The application I'm writing depends heavily on key events. When you press enter in a specific text field, some other field opens, when you press tab, the text fields moves, etc. Of course I need to cover this in my cucumber integration tests. The &lt;a href="http://celerity.rubyforge.org/yard/Celerity/Element.html#fire_event-instance_method"&gt;Celerity API&lt;/a&gt; is a bit thin for this purpose: You can fire a, let's say, keydown event - but it doesn't pass through the specific key code.&lt;/p&gt;


&lt;p&gt;Actually it's easy when you know how. You can execute any javascript directly on the Culerity RemoteBrowserProxy object. In order to fire a specific key event, you have to create a KeyboardEvent, initialize it with whatever you want, and dispatch it on the element.&lt;/p&gt;


&lt;p&gt;Read up the details, especially the meaning of initKeyEvent's parameters &lt;a href="http://help.dottoro.com/ljbwbehw.php"&gt;in this Javascript API&lt;/a&gt;.&lt;/p&gt;


&lt;p&gt;I ended up with this cucumber step:&lt;/p&gt;


&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="co"&gt;When&lt;/span&gt; &lt;span class="rx"&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;span class="k"&gt;^I hit &amp;quot;([^&lt;/span&gt;&lt;span class="ch"&gt;\&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;]*)&amp;quot; in a text_field with id &amp;quot;([^&lt;/span&gt;&lt;span class="ch"&gt;\&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;]*)&amp;quot;$&lt;/span&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;/span&gt; &lt;span class="r"&gt;do&lt;/span&gt; |key, id|
  key_code = &lt;span class="r"&gt;case&lt;/span&gt; key
    &lt;span class="r"&gt;when&lt;/span&gt; &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;enter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
      &lt;span class="i"&gt;13&lt;/span&gt;
    &lt;span class="r"&gt;when&lt;/span&gt; &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;up&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
      &lt;span class="i"&gt;38&lt;/span&gt;
    &lt;span class="r"&gt;when&lt;/span&gt; &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;down&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
      &lt;span class="i"&gt;40&lt;/span&gt;
    &lt;span class="r"&gt;else&lt;/span&gt;
      &lt;span class="i"&gt;0&lt;/span&gt;
    &lt;span class="r"&gt;end&lt;/span&gt;
  &lt;span class="gv"&gt;$browser&lt;/span&gt;.execute_script(&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;event = document.createEvent(&amp;quot;KeyboardEvent&amp;quot;);&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;)
  &lt;span class="gv"&gt;$browser&lt;/span&gt;.execute_script(&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;event.initKeyEvent(&amp;quot;keydown&amp;quot;, true, false, document.window, false, false, false, false, &lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt; + key_code.to_s + &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;, 0)&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;)
  &lt;span class="gv"&gt;$browser&lt;/span&gt;.execute_script(&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;document.getElementById('&lt;/span&gt;&lt;span class="il"&gt;&lt;span class="idl"&gt;#{&lt;/span&gt;id&lt;span class="idl"&gt;}&lt;/span&gt;&lt;/span&gt;&lt;span class="k"&gt;').dispatchEvent(event)&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;)
  &lt;span class="co"&gt;When&lt;/span&gt; &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;I wait for the AJAX call to finish&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
&lt;span class="r"&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Note that this is meant to work for Firefox only. For IE you can do a similar thing with createEventObject, &lt;a href="http://help.dottoro.com/ljhlvomw.php"&gt;see here how&lt;/a&gt;.&lt;/p&gt;
</description>
      <pubDate>Sun, 22 Nov 2009 20:43:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:0bb79202-702f-46ab-a4c1-4a9aecaf11b8</guid>
      <comments>http://lenaherrmann.net/2009/11/22/testing-key-events-with-culerity#comments</comments>
      <category>cucumber</category>
      <category>culerity</category>
      <category>celerity</category>
      <category>testing</category>
      <category>javascript</category>
      <trackback:ping>http://lenaherrmann.net/trackbacks?article_id=7</trackback:ping>
      <link>http://lenaherrmann.net/2009/11/22/testing-key-events-with-culerity</link>
    </item>
    <item>
      <title>Couchapp / Culerity glitches, part 1</title>
      <description>&lt;p&gt;Do you know what's behind these buzzwords: &lt;a href="http://couchdb.apache.org"&gt;CouchDB&lt;/a&gt; / &lt;a href="http://github.com/couchapp/couchapp"&gt;Couchapp&lt;/a&gt; / &lt;a href="http://code.quirkey.com/sammy/"&gt;Sammy.js&lt;/a&gt;? Not really? Then please look at this article now: &lt;a href="http://www.quirkey.com/blog/2009/09/15/sammy-js-couchdb-and-the-new-web-architecture"&gt;Sammy.js, CouchDB, and the new web architecture&lt;/a&gt;. The title says it all.&lt;/p&gt;


&lt;p&gt;This setup happens to be the field of my diploma thesis, friendly sponsored and supported by &lt;a href="http://upstream-berlin.com"&gt;Upstream&lt;/a&gt;. During the next months I'll develop a super duper thing with these technologies, and then I'll write 80 scientific pages about it. Yees, I'm not that much looking forward to the second part! Until then, I'm planning to semi-regularly blog about my findings.&lt;/p&gt;


&lt;p&gt;I'm not allowed to share the code before I have the diploma in my hands. But, because my workmate &lt;a href="http://twitter.com/bionadeholunder"&gt;Frank Proessdorf&lt;/a&gt; is so jealous of what I do, he and I started working on an app with a similar setup on our Upstream Research Fridays, to try things out. &lt;a href="http://github.com/lenalena/jsdoodle"&gt;Here it is&lt;/a&gt;. Don't expect anything yet.&lt;/p&gt;


&lt;p&gt;Today I'm going to tell you about some details of my testing setup. My *cough* supervising tutor &lt;a href="http://twitter.com/langalex"&gt;Alexander Lang&lt;/a&gt; already wrote down &lt;a href="http://upstream-berlin.com/2009/10/25/testing-couchapps-with-cucumber-and-culerity"&gt;all there is to know yet&lt;/a&gt; about Test-Driven Development with Cucumber-Culerity-Celerity and Couchapps.&lt;/p&gt;


&lt;p&gt;But there are a few things that will make your life hard, when the almost perfect Rails testing world has spoiled you as much as me. One thing is that we have to deal with a lot of asynchrony. Sammy renders the page, loads the data from the couch, and whenever the data is there it gets rendered into the page. The best way to do this is via callbacks. I'm going to write another blog post about that soon.&lt;/p&gt;


&lt;p&gt;In the integration test the problem is that celerity doesn't behave as it claims it does:&lt;/p&gt;

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="gv"&gt;$browser&lt;/span&gt;.wait&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;is &lt;em&gt;supposed&lt;/em&gt; to make the test wait until everything is rendered completely. But for whatever reason it doesn't. The result is that your test fails because it searches for your expressions in an unfinished page.&lt;/p&gt;

&lt;p&gt;Because of that, in culerity we need something like:&lt;/p&gt;

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;sleep &lt;span class="fl"&gt;0.4&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;after every follow, press and go-to-path step. When using culerity in a Rails environment, this usually is enough. Sammy behaves a bit more unpredictable - it sometimes takes more than a whole second until everything is loaded.&lt;/p&gt;

&lt;p&gt;Solution? We can either increase the sleep time a lot, but that's not very nice, the features need long enough to run as it is. Fortunately we can use another method that's build into celerity: wait_while.&lt;/p&gt;

&lt;p&gt;I created a div "spinner" somewhere in index.html:
&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt; &lt;span class="ta"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="an"&gt;id&lt;/span&gt;=&lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;spinner&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;span class="ta"&gt;&amp;gt;&lt;/span&gt;&lt;span class="ta"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;You can also add an actual spinner gif here. Just take care to place the div outside of your sammy element selector ("#main" by default), because this div gets replaced all the time.&lt;/p&gt;

&lt;p&gt;We need to show that div before every route that gets run. In application.js:
&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;before(&lt;span class="kw"&gt;function&lt;/span&gt;() {
  &lt;span class="pd"&gt;$&lt;/span&gt;(&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;#spinner&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;).show();
});&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;And we need to hide it after your rendering is completed. I haven't found an ideal solution where to do that, it depends on the application you are writing. As a quick and dirty fix, you can add it as a last line in sammy's partial function, in sammy.js:&lt;/p&gt;

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;partial: &lt;span class="kw"&gt;function&lt;/span&gt;(path, data, callback) {
  &lt;span class="c"&gt;//...&lt;/span&gt;
  &lt;span class="pd"&gt;$&lt;/span&gt;(&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;#spinner&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;).hide();
},&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;I prefer to have more control about when I hide it, as my partial calls sometimes have nested callbacks. So I put it in application.js, resp. in my controllers, after each render call or in the partial callbacks. I admit this still sucks a bit.&lt;/p&gt;

&lt;p&gt;Finally, amend the cucumber step:&lt;/p&gt;

&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="co"&gt;When&lt;/span&gt; &lt;span class="rx"&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;span class="k"&gt;I wait for the AJAX call to finish&lt;/span&gt;&lt;span class="dl"&gt;/&lt;/span&gt;&lt;/span&gt; &lt;span class="r"&gt;do&lt;/span&gt;
  sleep &lt;span class="fl"&gt;0.2&lt;/span&gt;
  puts &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;Waiting for page to load ...&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt; &lt;span class="r"&gt;if&lt;/span&gt; &lt;span class="gv"&gt;$browser&lt;/span&gt;.div(&lt;span class="sy"&gt;:id&lt;/span&gt;, &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;spinner&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;).visible?
  &lt;span class="gv"&gt;$browser&lt;/span&gt;.wait_while { &lt;span class="gv"&gt;$browser&lt;/span&gt;.div(&lt;span class="sy"&gt;:id&lt;/span&gt;, &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;spinner&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;).visible?} 
&lt;span class="r"&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The browser still needs to sleep a bit before it actually finds the spinner div. But with this step in my common_culerity_steps.rb, the features finally run smoothly.&lt;/p&gt;</description>
      <pubDate>Wed, 04 Nov 2009 10:07:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:3e495ea7-1021-404e-b3ba-18267d409146</guid>
      <comments>http://lenaherrmann.net/2009/11/04/couchapp-culerity-glitches-part-1#comments</comments>
      <category>cucumber</category>
      <category>culerity</category>
      <category>celerity</category>
      <category>couchapp</category>
      <category>couchdb</category>
      <category>sammy.js</category>
      <category>javascript</category>
      <category>bug</category>
      <category>testing</category>
      <trackback:ping>http://lenaherrmann.net/trackbacks?article_id=6</trackback:ping>
      <link>http://lenaherrmann.net/2009/11/04/couchapp-culerity-glitches-part-1</link>
    </item>
  </channel>
</rss>
