2015-09-15 9 views
5

sails.jsアプリケーションのユニットテストが期待どおりに機能しない理由を知りたいと思っています。Bluebird promiseがmocha/chaiテストで動作しない

私はmocha、chai、bluebird約束ライブラリをsails.jsアプリで使用しています。私が達成したい何

  • 名 パラメータを受け入れTagsService.create(名)メソッドのためのテストを作成します。この方法は、私が名前のパラメータが必要で、私が現在持っている何

未満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) { 
     
    \t \t 
     
        // 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(); 
     
         }); 
     
        
     
    \t \t 
     
        }); 
     
        
     
    });

    私が有効な名前を渡すか、メソッドからnullを返すとしても、それは合格と思われます。

  • 答えて

    5

    多くの試行錯誤の末、私はそれを解決できたようです。

    私はそれぞれのメソッドを実行した後、Promiseからdone()コールバックをキャッチする必要があります。また、TagsService promiseオブジェクトから実行されたテストの結果を返す必要があります。 (これは100%確信していませんが、これについて考える正しい方法です)。とにかく、テストは今正しく機能しているようです。ここで

    は私の結果である:

    var names = ['',' ','thisstringislongerthanthemaxof121characterslongthisstringislongerthanthemaxof121characterslongthisstringislongerthanthema',[],[{}],[{test: 'test'}],'wrongchars*[]$%fsf','$%@~}[','�$%jkdfi',' $%"�asdwdFDE','hD8U �$&{DS ds']; 
     
    \t \t \t 
     
    sails.bluebird.each(names, function(name){ 
     
        return TagsService.create(name).then(function(data) { 
     
    \t assert.property(data, 'status', 'create method did not return a status property'); 
     
    \t 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(){ 
     
    \t done(); 
     
    }).catch(done);

    +0

    はhttps://github.com/domenic/chai-as-promisedも参照してください。 –

    関連する問題