Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Kanso apps are CouchDB apps. However the best bang-for-buck is to ignore CouchDB for now. The important thing is this: <strong>Kanso apps are Node.js apps</strong>. Test them the same way you would test a Node.js app. Test that they adhere to the documented CouchDB API and you will be fine.</p> <p>Ideally, we might want to run tests <em>actually in CouchDB</em>. The JavaScript engines are different (V8 vs. SpiderMonkey); the environments are different. However in practice, it is so much easier to test Node.js code. (Also, a whole class of JavaScript bugs are absent on both platforms: third-party code setting global variables, changing built-in types, changing prototypes&mdash;those are all browser issues. Node.js and CouchDB are both pristine and predictable.)</p> <h2>Example</h2> <p>Let's make a simple Couch app that outputs "Hello world" in a <a href="http://wiki.apache.org/couchdb/Formatting_with_Show_and_List">_show function</a>.</p> <p>The <code>kanso.json</code> file:</p> <pre><code>{ "name" : "hello_world" , "version": "0.1.0" , "description": "A simple hello-world Couch app" , "dependencies": { "node-couchapp": "~0.8.3" } , "app": "app" } </code></pre> <p>Next run <code>kanso install</code> which will pull in the "node-couchapp" dependency. (Notice how using the <code>kanso</code> command is similar to using the <code>npm</code> command.)</p> <p>Let's make a very simple Couch app, in <code>./app.js</code>:</p> <pre><code>// A Couch app that just says hello in a _show function. module.exports = { 'shows': { 'hello': function(doc, req) { var who = req.query.who || "world" return "Hello, " + who } } } </code></pre> <p>I ran <code>kanso push http://example.iriscouch.com/so_hello</code> and I can see my app here:</p> <ul> <li><a href="http://example.iriscouch.com/so_hello/_design/hello_world/_show/hello">http://example.iriscouch.com/so_hello/_design/hello_world/_show/hello</a></li> <li><a href="http://example.iriscouch.com/so_hello/_design/hello_world/_show/hello?who=Stack+Overflow">http://example.iriscouch.com/so_hello/_design/hello_world/_show/hello?who=Stack+Overflow</a></li> </ul> <h2>Adding Tests</h2> <p>I like <a href="https://github.com/isaacs/node-tap">node-tap</a> so let's use that. But the main point is, this is just some Node.js code. Test it using whatever method your prefer.</p> <p>First, a quick <code>package.json</code> file:</p> <pre><code>{ "name" : "hello_world" , "description": "A simple hello-world Couch app" , "version": "0.1.0" , "private": true , "devDependencies": { "tap": "~0.2.3" } } </code></pre> <p>Run <code>npm install</code> to get the node-tap package. (And I always have <code>./node_modules/.bin</code> in my <code>$PATH</code> when I work on Node.js. Rather than a global install, I like to have everything I need right there in the project.</p> <p>Next, perhaps a <code>test/show_function.js</code> file:</p> <pre><code>var tap = require('tap') tap.test('The Couch app loads', function(t) { t.doesNotThrow(load_app, 'No problem loading the app.js file') t.end() function load_app() { var app = require('../app') } }) tap.test('The show function', function(t) { var app = require('../app') , hello = app.shows.hello t.type(hello, 'function', 'Show function "hello" in the couch app') var doc = {} , null_req = {'query':{}} , john_req = {'query':{'who':'John Doe'}} t.equal(hello(doc, null_req), 'Hello, world', '"Hello world" by default') t.equal(hello(doc, john_req), 'Hello, John Doe', 'Supports ?who query string') t.end() }) </code></pre> <p>Test it by running <code>tap test</code>:</p> <pre><code>$ tap test ok test/show_function.js ................................ 5/5 total ................................................... 5/5 ok </code></pre> <p>I'll change the code to return "Hello, world" hard-coded (i.e., ignore the <code>req.query.who</code> parameter). Notice the failing test:</p> <pre><code>$ tap test not ok test/show_function.js ............................ 4/5 Command: "node" "show_function.js" ok 1 No problem loading the app.js file ok 2 Show function "hello" in the couch app ok 3 "Hello world" by default not ok 4 Supports ?who query string --- file: /private/tmp/j/test/show_function.js line: 23 column: 5 stack: - getCaller (/private/tmp/j/node_modules/tap/lib/tap-assert.js:403:17) - assert (/private/tmp/j/node_modules/tap/lib/tap-assert.js:19:16) - Function.equal (/private/tmp/j/node_modules/tap/lib/tap-assert.js:160:10) - Test._testAssert [as equal] (/private/tmp/j/node_modules/tap/lib/tap-test.js:86:16) - Test.&lt;anonymous&gt; (/private/tmp/j/test/show_function.js:23:5) - Test.&lt;anonymous&gt; (native) - Test.&lt;anonymous&gt; (events.js:88:20) - Test.emit (/private/tmp/j/node_modules/tap/lib/tap-test.js:103:8) - GlobalHarness.&lt;anonymous&gt; (/private/tmp/j/node_modules/tap/lib/tap-harness.js:86:13) - Array.0 (native) found: Hello, world wanted: Hello, John Doe diff: | FOUND: Hello, world WANTED: Hello, John Doe ^ (at position = 7) ... ok 5 test/show_function.js 1..5 # tests 5 # pass 4 # fail 1 total ................................................... 4/5 not ok </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload