2013-03-27 125 views
10

我有以下testrunner.html摩卡初始化超時

<html> 
    <head> 
    <title>Specs</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
    <link rel="stylesheet" href="/content/css/mocha.css" /> 
    <script> 
     function assert(expr, msg) { 
      if (!expr) throw new Error(msg || 'failed'); 
     } 
    </script> 

    <script src="/client/lib/require.js" type="text/javascript" data-main="/client/specs/_runner.js"></script> 

    </head> 
    <body> 
    <div id="mocha"></div> 
    </body> 
</html> 

_runner.js看起來是這樣的:

// Configure RequireJS 
require.config({ 
    baseUrl: '/client', 
    urlArgs: "v=" + (new Date()).getTime() 
}); 

// Require libraries 
require(['require', 'lib/chai', 'lib/mocha'], function (require, chai) { 

    // Chai 
    assert = chai.assert; 
    should = chai.should(); 
    expect = chai.expect; 

    // Mocha 
    mocha.setup('bdd'); 


    // Require base tests before starting 
    require(['specs/stringcalculator.specs'], function (person) { 
     mocha.setup({ globals: ['hasCert'] }); 
     // Start runner 
     if (window.mochaPhantomJS) { 
      mochaPhantomJS.run(); 
     } 
     else { mocha.run(); } 
    }); 

}); 

StringCalculator.specs.js是這樣的:

define(['app/model/StringCalculator'], function() { 

    describe("StringCalculator", function() { 

     describe("when an empty string is passed in", function() { 
      it("returns 0", function() { 
       var result = StringCalculator.add(""); 
       assert(result === 0); 
      }); 
     }); 

     describe("when a number is passed in", function() { 
      it("returns the number", function() { 
       var result = StringCalculator.add("2"); 
       assert(result === 2); 
      }); 
     }); 

     describe("when string is passed in", function() { 
      it("returns NaN", function() { 
       var result = StringCalculator.add("a"); 
       assert(isNaN(result)); 
      }); 
     }); 

     describe("when '1,2' is passed in", function() { 
      it("returns 3", function() { 
       var result = StringCalculator.add("1,2"); 
       assert(result === 3); 
      }); 
     }); 
    }); 
}); 

這是StringCalculator.js本身(來自摩卡樣本):

define([], function() { 
    window.StringCalculator = StringCalculator = { 
     add: function(inputString) { 
      if (inputString === '') { 
       return 0; 
      } 

      var result = 0; 
      var inputStrings = inputString.split(','); 

      for (var i = 0; i < inputStrings.length; i++) { 
       result += parseInt(inputStrings[i]); 
      } 

      return result; 
     } 
    } 
}); 

在調用testrunner.html的瀏覽器中運行規格時,一切都按預期工作。 當在OS X上運行mocha-phantomjs client/specs/testrunner.html,我得到以下錯誤:

Failed to start mocha: Init timeout

什麼可我在這裏失蹤?

我也試過mocha-phantomjs http://httpjs.herokuapp.com,它的失敗與相同的錯誤。

更新: 如果我打電話mocha-phantomjs http://localhost:81/client/specs/testrunner.html我也收到以下錯誤控制檯上:

RangeError: Maximum call stack size exceeded. 

http://localhost:81/client/lib/chai.js?v=123423553533535:2601 
Failed to start mocha: Init timeout 

回答

1

This file展示瞭如何使用它。

而對於我來說,NodeJS 0.10.x似乎並不適用於它。切換到NodeJS 0.8.8後,一切都按預期工作。

使用當前版本的mocha-phantomjs和PhantomJS現在一切正常。

+0

節點0.10.x用戶不完全泄氣 - 我敢肯定,這是真的在當時。但今天我在0.10.13上面的解決方案使用mochaPhantomJS.run()爲我工作。 – laurelnaiad 2013-08-31 04:55:37

+0

我仍然會隨機超時(無雙關語)。使用最新的mocha-phantomjs等,超時時間爲10000. – Till 2014-02-03 18:21:56

9

當通過grunt-mocha-phantomjs npm包運行mocha-phantomjs時,我得到相同的Failed to start mocha錯誤。找到解決方案here

這裏,以供參考:

要與摩卡phantomjs運行,改變

mocha.run(); 

if (mochaPhantomJS) { 
    mochaPhantomJS.run(); 
} 
else { 
    mocha.run(); 
} 
+0

嗨,我也看到了這個問題。但我只是使用npm安裝了Mocha-phantomjs和phantomjs。我不知道我在哪裏可以找到SpecRunner.js以及它究竟做了什麼。你能解釋一下嗎? – EternallyCurious 2014-01-12 12:21:30

+0

@EternallyCurious,我不確定你的問題是否在這個範圍內。打開一個新的? – mnoble01 2014-01-27 23:05:29

+0

該鏈接現在是https://gist.github.com/michaelcox/3800736/#gistcomment-859304 (#comment已更改爲#gistcomment) – 2015-10-08 11:34:34

0

感謝這個信息,我想以上,但它在瀏覽器中失敗說「mochaPhantomJS是未定義的」。根據下面的快速調整,它運作良好:

if(typeof(mochaPhantomJS)!=="undefined") 
{ 
    mochaPhantomJS.run(); 
} 
else 
{ 
    mocha.run(); 
}