Я не могу понять, почему мой обратный вызов сохранения вызывается дважды в моем тесте мокко, когда обратный вызов терпит неудачу. Он не вызывает сохранение дважды, он только снова запускает обратный вызов для сохранения, но с ошибкой «следует», когда мой второй модульный тест терпит неудачу. Если я уберу ошибочное утверждение «следует» should.exist err
из второго теста, он, похоже, будет работать нормально и не будет дважды запускать обратный вызов сохранения индекса.
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
мокко тест
describe "User", ->
it "should be able to save", (done) ->
user = new User("quark", "[email protected]", "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", "[email protected]", "profit")
user.save (err, result) ->
console.log err
should.exist err #this triggers the user.save callback to be fired twice
done null
И результаты испытаний
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)