使用 Vows 测试 Mongoose 模型

发布于 2024-12-02 11:29:02 字数 841 浏览 0 评论 0原文

对于整个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

虫児飞 2024-12-09 11:29:02

誓言不是这样运作的。誓言不能是异步的。您应该使用子主题进行异步测试

伪代码(我无法编写CS)

topic: () -> new Project()
'should have name': {
  'topic': (topic) ->
    topic.name = "some name"
    topic.save
    Project.findById topic.id, this.callback
    return;
  'that can be saved': (err, proj) ->
    console.log "SHOULD RUN"
    assert.equal "some name", proj.name
}

正如您所看到的,您使用执行异步活动的主题创建了一个新上下文。然后,您对异步活动返回的数据发誓。

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)

topic: () -> new Project()
'should have name': {
  'topic': (topic) ->
    topic.name = "some name"
    topic.save
    Project.findById topic.id, this.callback
    return;
  'that can be saved': (err, proj) ->
    console.log "SHOULD RUN"
    assert.equal "some name", proj.name
}

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.

ゃ懵逼小萝莉 2024-12-09 11:29:02

我发现的一个问题是 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 meant topic.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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文