蓝鸟承诺每个在摩卡/柴测试不工作

mrp*_*tem 5 mocha.js chai sails.js bluebird

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

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

我想要实现的目标:

  • 为TagsService.create(name)方法创建一个测试,该方法接受一个name参数.
  • 测试此方法不会根据我传递的无效名称创建新的标记记录
  • name参数是必需的,长度应小于121个字符

我目前拥有的:

// 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();
      });
    
		
   });
  
});
Run Code Online (Sandbox Code Playgroud)

即使我传入有效名称或从方法返回null,它似乎也会传递.

mrp*_*tem 5

好吧,看起来我经过多次试验和错误后设法解决了这个问题.

结果我需要在执行每个方法后从Promise捕获done()回调.还需要返回从TagsService promise对象完成的测试结果.(仍然不是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);
Run Code Online (Sandbox Code Playgroud)