Question

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?

Était-ce utile?

La solution

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.

Autres conseils

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.)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top