Kanso 应用是 CouchDB 应用。然而,最好的选择是暂时忽略 CouchDB。重要的是:Kanso 应用程序是 Node.js 应用程序。以与测试 Node.js 应用程序相同的方式测试它们。测试它们是否遵守记录在案的 CouchDB API,你会没事的。
理想情况下,我们可能希望实际上在 CouchDB 中运行测试。 JavaScript 引擎不同(V8 与 SpiderMonkey);环境不同。然而在实践中,测试 Node.js 代码要容易得多。 (此外,两个平台上都没有一整类 JavaScript 错误:第三方代码设置全局变量、更改内置类型、更改原型——这些都是浏览器问题。Node.js 和 CouchDB 都是原始且可预测的。)
示例
让我们制作一个简单的 Couch 应用,在 _show function 中输出“Hello world”。
kanso.json 文件:
{ "name" : "hello_world"
, "version": "0.1.0"
, "description": "A simple hello-world Couch app"
, "dependencies": { "node-couchapp": "~0.8.3" }
, "app": "app"
}
接下来运行kanso install,它将引入“node-couchapp”依赖项。 (注意使用kanso 命令与使用npm 命令的方式相似。)
让我们在./app.js 中制作一个非常简单的 Couch 应用:
// 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
}
}
}
我跑了kanso push http://example.iriscouch.com/so_hello,我可以在这里看到我的应用:
添加测试
我喜欢node-tap,所以让我们使用它。但重点是,这只是一些 Node.js 代码。使用您喜欢的任何方法对其进行测试。
首先,一个快速的package.json 文件:
{ "name" : "hello_world"
, "description": "A simple hello-world Couch app"
, "version": "0.1.0"
, "private": true
, "devDependencies": { "tap": "~0.2.3" }
}
运行npm install 获取node-tap 包。 (当我在 Node.js 上工作时,我的 $PATH 中总是有 ./node_modules/.bin。我喜欢在项目中拥有我需要的一切,而不是全局安装。
接下来,可能是test/show_function.js 文件:
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()
})
通过运行tap test来测试它:
$ tap test
ok test/show_function.js ................................ 5/5
total ................................................... 5/5
ok
我将更改代码以返回硬编码的“Hello, world”(即忽略req.query.who 参数)。注意失败的测试:
$ 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.<anonymous> (/private/tmp/j/test/show_function.js:23:5)
- Test.<anonymous> (native)
- Test.<anonymous> (events.js:88:20)
- Test.emit (/private/tmp/j/node_modules/tap/lib/tap-test.js:103:8)
- GlobalHarness.<anonymous> (/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