使用 Vows 测试 Mongoose 模型
对于整个 Node.js 社区来说相当陌生,我在第一个应用程序上进行单元测试时遇到了麻烦。问题是它们通过了,但它们从未在回调中实际运行断言。据我了解,mongoose(我用来与 MongoDB 对话的库)使用回调来处理它的 API。在我的誓言测试中,这些回调似乎没有被解雇。举个例子:
vows = require 'vows'
assert = require 'assert'
mongoose = require 'mongoose'
ProjectSchema = new Schema
name: String
Project = mongoose.model 'Project', ProjectSchema
mongoose.connect('mongodb://localhost/testdb');
projectBatch = vows.describe('Project').addBatch
'model attributes':
topic: ()->
new Project()
'should have a name field': (topic)->
topic.name = "some name"
topic.save
console.log "this gets executed just fine"
Project.findById topic.id, (error, project)->
console.log "THIS LINE NEVER RUNS!"
assert.equal "some name", project.name
projectBatch.export module
关于我在这里做错了什么有什么想法吗?
Fairly new to the whole node.js community, and I'm having trouble with my unit tests on my first app. The problem is they pass, but they never actually run the assertions in the callbacks. As I understand it, mongoose (the library I'm using to talk to MongoDB) uses callbacks for working with it's API. In my vows tests, these callbacks don't seem to get fired. An example:
vows = require 'vows'
assert = require 'assert'
mongoose = require 'mongoose'
ProjectSchema = new Schema
name: String
Project = mongoose.model 'Project', ProjectSchema
mongoose.connect('mongodb://localhost/testdb');
projectBatch = vows.describe('Project').addBatch
'model attributes':
topic: ()->
new Project()
'should have a name field': (topic)->
topic.name = "some name"
topic.save
console.log "this gets executed just fine"
Project.findById topic.id, (error, project)->
console.log "THIS LINE NEVER RUNS!"
assert.equal "some name", project.name
projectBatch.export module
Any ideas on what I'm doing wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
誓言不是这样运作的。誓言不能是异步的。您应该使用子主题进行异步测试
伪代码(我无法编写CS)
正如您所看到的,您使用执行异步活动的主题创建了一个新上下文。然后,您对异步活动返回的数据发誓。
That's not how vows works. vows cannot be asynchronous. You should use sub topics for asynchronous testing
Pseudo code (I can't write CS)
As you can see you create a new context with a topic that does asynchronous activity. You then vow things about the data your asynchronous activity returns.
我发现的一个问题是
topic.save
是一项无操作 — 您可能指的是topic.save()
。另一个更严重的问题是,您需要使用 Vows 的this.callback
进行异步测试;请参阅雷诺斯的回答。您还需要注意,当 Vows 获得任何返回值(
undefined
除外,这相当于不返回任何内容)时,它就会结束测试。由于 CoffeeScript 的隐式返回,这意味着您必须非常小心。 (我记录为支持没有返回值的函数的替代-/> 语法;请参阅 问题 899。)
One problem I see is that
topic.save
is a no-op—you probably meanttopic.save()
. Another, more serious problem is that you need to use Vows'this.callback
for async tests; see Raynos' answer.You also need to be aware that Vows ends a test when it gets any return value (other than
undefined
, which is equivalent to not returning anything). Because of CoffeeScript's implicit returns, this means you have to be very careful. (I'm on record as supporting an alternate-/>
syntax for functions with no return value; see issue 899.)