2015-06-12 76 views
1

這是有問題的代碼。Backbone.Deferred.Model:爲什麼unittest沒有運行?

define (require) -> 

    Backbone = require 'backbone' 

    require 'backbone-deferred' 

    class Data extends Backbone.Deferred.Model 

    urlRoot: 'data' 

    parse: (resp) -> 
     resp.record or resp 

    isValid: -> 
     @get 'valid' 

    # Newly introduced 
    dataUrl: -> 
     @get('data_url')?.replace /abc/, '' 

這裏是單元測試

define (require) -> 

    Data   = require 'model/Data' 

    describe 'dataurl', -> 

    describe 'No change', -> 
     data = new Data id: '123', name: 'Dummy', data_url: 'https://m/a.png' 
     expect(data.dataUrl()).to.equal('https://m/a.png') 

出於某種原因,測試並非由摩卡運行。

我使用這個網站來驅動測試

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8"> 
    <title>Mocha Tests</title> 
    <link rel="stylesheet" href="../bower_components/mocha/mocha.css" type="text/css" media="screen" title="Tests for {%= name %}" charset="utf-8"> 
</head> 
<body> 
    <div id="mocha"></div> 
    <script src="../bower_components/mocha/mocha.js" type="text/javascript" charset="utf-8"></script> 
    <script src="../bower_components/chai/chai.js" type="text/javascript" charset="utf-8"></script> 

    <script src="../bower_components/requirejs/require.js" type="text/javascript" charset="utf-8"></script> 
    <!-- Requirejs configuration --> 
    <script src="../temp/local/scripts/config.js" type="text/javascript" charset="utf-8"></script> 

    <script type="text/javascript" charset="utf-8"> 
    // Dumping console messages to /dev/null to avoid parsing error 
    // when grunt:mocha_phantomjs is run 
    if (navigator.userAgent.indexOf('PhantomJS') >= 0) { 
     var log = console.log 
     console.log = function(message) { 
     if (message.indexOf('<') === 0) { 
      log.apply(this, Array.prototype.slice.call(arguments)) 
     } 
     } 
    } 

    // Mixpanel is not loaded for unit testing. 
    // Redirecting mixpanel to /dev/null. 
    window.mixpanel = { 
     track: function() {} 
    }; 

    require.config({ 
     baseUrl: "../temp/local/scripts", 
     shim: { 
     'chai-jquery': { 
      deps: ['jquery'] 
     }, 
     'sinon': { 
      exports: 'sinon' 
     } 
     }, 
     paths: { 
     // Aliasing path to find the specs 
     'specs': '../../specs', 

     'sinon'  : '../../../bower_components/sinon/lib/sinon', 
     'sinon-chai' : '../../../bower_components/sinon-chai/lib/sinon-chai', 
     'chai-jquery': '../../../bower_components/chai-jquery/chai-jquery', 
     'handlebars' : '../../../bower_components/handlebars/handlebars', 

     } 
    }); 

    runTests = function() { 

     // Ensuring that jquery and chai-jquery are loaded 
     require(
      ['jquery', 'chai-jquery', 'sinon-chai', 'handlebars'], 
      function($, jqueryChai, sinonChai, Handlebars) { 
     // Registering testing style in globals for easy access 
     window.expect = chai.expect; 
     chai.use(jqueryChai); 
     chai.use(sinonChai) 

     mocha.setup({ 
      ui: 'bdd', 
      ignoreLeaks: true 
     }); 

     // Protect from barfs 
     console = window.console || function() {}; 

     // Don't track 
     window.notrack = true; 

     // Helper... not really needed but in case we want to do something fancy 
     var runMocha = function() { 
      mocha.run(); 
     }; 

     require([ 
      // Many other tests; removed for clarity 
      'specs/data.spec' 
     ], function() { 
      // For xUnit and code coverage reports, we run the tests 
      // using mocha-phantomjs runner. 
      if (window.mochaPhantomJS) { 
      mochaPhantomJS.run(); 
      } 
      else { 
      mocha.run(); 
      } 
     }); 
     }); 

    } 

    </script> 
</body> 
</html> 

這裏是繁重的測試任務的輸出

Running "mocha:default" (mocha) task 
Testing: test/index.html 


    ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․ 
    ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․ 
    ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․ 
    ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․ 
    ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․ 
    ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․ 
    ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․ 
    ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․ 
    ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․ 
    ․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․․ 
    ․․․․․․․․․․․ 

    1081 passing (9s) 

>> 1081 passed! (9.17s) 

我的問題是:

1)測試的數目是在添加新測試後完全改變。新測試沒有被拿起的可能原因是什麼?

2)如果我故意在我的單元測試引入誤差就會造成這在測試失敗的情況下:

Warning: PhantomJS timed out, possibly due to a missing Mocha run() call. Use --force to continue. 

我相信這是關係到測試,但沒有足夠的信息來診斷問題。我如何開啓日誌記錄或獲取更多信息?

回答

1

你的測試用例應該用it函數,而不是describe(這是用來測試分組)

define (require) ->  
    Data = require 'model/Data'  
    describe 'dataurl', ->  
    it 'No change', -> 
     data = new Data id: '123', name: 'Dummy', data_url: 'https://m/a.png' 
     expect(data.dataUrl()).to.equal('https://m/a.png') 

我創造了this jdfiddle所以你可以看到你的原始定義的不同定義。

如果在測試中引入錯誤,如果在it函數內沒有定義測試,則會影響整個測試運行。您可以在other fiddle(您會注意到第二次測試在錯誤定義的測試錯誤後不運行)

相關問題