4

I can't figure out why my save callback is being called twice in my mocha test when the callback fails. It doesn't call the save twice, it only fires the callback for the save again but with the 'should' error when my 2nd unit test fails. If I take out the failing 'should' assertion should.exist err from the second test it seems to work fine and doesn't trigger the save index callback twice.

class User

  constructor : (@name, @email, @pwd) ->
  save : (callback) ->
    unless this.validate()
      throw new Error('invalid data')
    else
      user = 
        name  : @name
        email : @email
        pwd   : @pwd

      node = db.createNode user

      node.save (err) ->
        unless err        
          user.id = node.id;
          node.index 'User', 'name', user.name.toLowerCase(), (err2) ->
            #why is this being fired twice when an assert in the callback fail?
            console.log '----------- triggering the save callback'
            callback err2, user

        else
          callback err

the mocha test

describe "User", ->
    it "should be able to save", (done) ->
        user = new User("quark", "quark@ds9.com", "profit")
        user.save (err, result) ->
            should.exist result
            done err

    #this second unit test should fail since the duplicate checking is not yet implemented
    it "should not allow duplicates to be saved", (done) ->
        user = new User("quark", "quark@ds9.com", "profit")
        user.save (err, result) ->
            console.log err
            should.exist err #this triggers the user.save callback to be fired twice
            done null

And the test results

  User
    ◦ should be able to save: ----------- triggering the save callback
    ✓ should be able to save (43ms)
    ◦ should not allow duplicates to be saved: ----------- triggering the save callback
undefined
----------- triggering the save callback
{ name: 'AssertionError',
  message: 'expected undefined to exist',
  actual: undefined,
  expected: undefined,
  operator: undefined }
    ✓ should not allow duplicates to be saved 


  ✔ 2 tests complete (69ms)
John Gietzen
  • 48,783
  • 32
  • 145
  • 190
MonkeyBonkey
  • 46,433
  • 78
  • 254
  • 460

1 Answers1

1

Well, first, having tests that have a predefined order is poor form. Your second test should attempt to save two users to the database rather than relying on the first test.

Second, I can only assume that db is a node-neo4j Database in this context and that your assertion framework (should.js? chai?) is using exceptions. So my answer is based on that assumption.

It seems that node-neo4j is recalling the callback function in the case of an exception.

Try just doing a throw 'blah' instead of a should assertion to see if you can narrow it down to that. This isn't listed in the node-neo4j documentation, so it seems like a bug.

See: http://coffeedoc.info/github/thingdom/node-neo4j/master/classes/Node.html#save-instance

John Gietzen
  • 48,783
  • 32
  • 145
  • 190