Bluebird Promise 每个 mocha/chai 测试都不起作用

Bluebird Promise each in mocha/chai test not working

我需要一些帮助来确定为什么我在 sails.js 应用程序中的单元测试没有按预期工作。

我在 sails.js 应用程序上使用 mocha、chai 和 bluebird promise 库。

我想达到的效果:

我目前拥有的:

// Test the 'create' method
describe('Method \'create\' test result: \n', function () {
  
  // Test that name is required and less than 121 chars long
  it('Must receive the name parameter and be less than 121 chars long', function(done) {
  
    // It should not accept any of the following names
    var names = ['',' ','thisstringislongerthanthemaxof121characterslongthisstringislongerthanthemaxof121characterslongthisstringislongerthanthema',[],[{}],[{test: 'test'}],'wrongchars*[]$£%fsf','$%@~}[','£$%jkdfi',' $%"£asdwdFDE','hD8U £$&{DS ds'];
    
    
      sails.bluebird.each(names,function(name){
        TagsService.create(name).then(function(data){
          assert.propertyVal(data,'status','err','An error was NOT returned - even though names provided should be invalid');
        });
      }).then(function(){
        done();
      });
    
  
   });
  
});

即使我从方法中传入有效名称或 return null,它似乎也通过了。

好吧,经过多次尝试和错误,看来我设法解决了它。

原来我需要在每个方法执行后从 Promise 捕获 done() 回调。还需要 return 从 TagsService 承诺对象完成的测试结果。 (仍然不能 100% 确定这是正确的思考方式……)。无论如何,测试现在似乎可以正常运行。

这是我的结果:

var names = ['',' ','thisstringislongerthanthemaxof121characterslongthisstringislongerthanthemaxof121characterslongthisstringislongerthanthema',[],[{}],[{test: 'test'}],'wrongchars*[]$%fsf','$%@~}[','�$%jkdfi',' $%"�asdwdFDE','hD8U �$&{DS ds'];
   
sails.bluebird.each(names, function(name){
    return TagsService.create(name).then(function(data) {
 assert.property(data, 'status', 'create method did not return a status property');
 assert(data.status === 'err', 'even with an invalid name parameter passed - it did not return an err status, which it must do with an invalid name.');
    });
}).then(function(){
 done();
}).catch(done);