jasmine-html不在浏览器中运行,但在控制台中运行

时间:2014-09-11 09:13:31

标签: javascript angularjs unit-testing jasmine yeoman

我有一个与yeoman建立的Angularjs项目,除了单元测试部分外,一切正常。测试在控制台模式下工作(当我在cmd中使用grunt servegrunt test时)但是当我尝试运行runner.html文件以在浏览器上查看时,它没有测试任何规格。 我的package.json依赖项看起来像这样:

"devDependencies": {
    "grunt": "~0.4.5",
    ....
    "karma-ng-scenario": "^0.1.0",
    "grunt-karma": "^0.8.3",
    "karma": "^0.12.23",
    "karma-ng-html2js-preprocessor": "^0.1.0",
    "karma-jasmine": "^0.2.2",
    "karma-firefox-launcher": "^0.1.3",
    "karma-chrome-launcher": "^0.1.4",
    "grunt-ng-annotate": "^0.3.2",
    "grunt-angular-templates": "^0.5.7"
}

并且在我的runner.html中就是这样

<!doctype html>
<html lang="en">

<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Jasmine Spec Runner v2.0.3</title>

    <link rel="shortcut icon" type="image/png" href="lib-jasmine-2.0.3/jasmine_favicon.png">
    <link rel="stylesheet" type="text/css" href="lib-jasmine-2.0.3/jasmine.css">

    <script type="text/javascript" src="lib-jasmine-2.0.3/jasmine.js"></script>
    <script type="text/javascript" src="lib-jasmine-2.0.3/jasmine-html.js"></script>
    <script type="text/javascript" src="lib-jasmine-2.0.3/boot.js"></script>


    <!-- include source files here... -->
    <!-- my angular and other libs scripts here -->


    <!-- include spec files here... -->
    <script src="mock/stateMock.js"></script>
    <script src="spec/unit/controllers/main.js"></script>
    <script src="spec/unit/services/survey.js"></script>

</head>

<body>
</body>

</html>

如何让它像在控制台中一样运行?提前致谢

1 个答案:

答案 0 :(得分:0)

假设您对规范的引用是正确的(它们位于您的runner.html下方,可能只是您需要包含源文件和供应商特定文件。这是您上面和我的内容之间的唯一区别我没有通过我的规范运行器解决的一个问题是使用templateUrls的指令。这是使用karma-ng-html2js-preprocessor插件在karma中解决的,但是当你使用jasmine runner时它不会运行。这是一个我的缩短版本供我参考。仅供参考。我改变了我的yeoman生成器,在添加文件时也在此文件上运行wiredep,以便自动添加所有文件。节省了很多麻烦。

<html>
<head>
  <meta charset="utf-8">
  <title>Jasmine Spec Runner</title>
  <link rel="stylesheet" type="text/css" href="bower_components/jasmine/lib/jasmine-core/jasmine.css">
</head>
<body>
    <script src="bower_components/jasmine/lib/jasmine-core/jasmine.js"></script>
    <script src="bower_components/jasmine/lib/jasmine-core/jasmine-html.js"></script>
    <script src="bower_components/jasmine/lib/jasmine-core/json2.js"></script>
    <script src="bower_components/jasmine/lib/jasmine-core/boot.js"></script>
    <!-- bower:js -->
    <script src="bower_components/angular/angular.js"></script>
    ... all other bower components
    <!-- endbower -->
    <!-- devDependencies -->
    <script src="bower_components/angular-mocks/angular-mocks.js"></script>
    <!-- enddevDependencies -->
    <!-- build:js({.tmp,app}) scripts/scripts.js -->
    <script src="app/scripts/app.js"></script>
    ..all other source files
    <!-- endbuild -->
    <!-- specs -->
    <script src="test/spec/controllers/myspec.js"></script>
    <!-- endspecs -->
</body>