【问题标题】:Why do these resharper Jasmine tests run in browser but not in phantomjs为什么这些 resharper Jasmine 测试在浏览器中运行而不在 phantomjs 中运行
【发布时间】:2013-09-03 13:41:36
【问题描述】:

提前感谢所有阅读本文的人 - 这个问题有很多细节。

我正在尝试使用 Jasmine 将 javascript 测试引入我们的项目。我编写的测试在浏览器中工作,但没有通过 Resharper 使用 PhantomJS。

我猜我错过了解析 JS 文件所需的管道(也许我发现我们的 JS 设置存在问题)。任何帮助都非常感谢...

我在 Visual Studio 中使用 jasmine 设置了 Jasmine 测试...

// The Jasmine Test Framework
/// <reference path="../jquery-1.10.1.js"/>
/// <reference path="lib/jasmine-1.3.1/jasmine.css"/>
/// <reference path="lib/jasmine-1.3.1/jasmine.js"/>
/// <reference path="lib/jasmine-1.3.1/jasmine-html.js"/>
/// <reference path="lib/jasmine-jquery.js"/>
/// <reference path="lib/mock-ajax.js"/>
// Classes to test
/// <reference path="../MyNamespace.Navigation.js"/>

describe("Breadcrumbs", function () {
    it("should have a window object", function() {
        //this test passes in PhantomJS and the browser
        expect(window).toBeDefined();
    });

    it("should have the base object available", function() {
        //this test only passes in the browser
        expect(window.MyNamespace).toBeDefined();
    });
});

这引用了一个包含...的 JS 文件

(function (app, $, undefined) {
     //do things here
}(window.MyNameSpace = window.MyNamespace || {}, jQuery));

我有一个 SpecRunner.html 可以成功运行我的测试...它是 Jasmine 1.3.1 独立附带的 specrunner html,其头部编辑如下...

 <link rel="stylesheet" type="text/css" href="lib/jasmine-1.3.1/jasmine.css">
  <script type="text/javascript" src="../jquery-1.10.1.js"></script>
  <script type="text/javascript" src="lib/jasmine-1.3.1/jasmine.js"></script>
  <script type="text/javascript" src="lib/jasmine-1.3.1/jasmine-html.js"></script>

  <!-- include source files here... -->
  <script type="text/javascript" src="../MyNamespace.Navigation.js"></script>

  <!-- include spec files here... -->
  <script type="text/javascript" src="spec/BreadcrumbsSpec.js"></script>

这些测试由我的 SpecRunner.html(运行良好)或使用 PhantomJS 的 Resharper 7s 测试运行程序运行,我猜这不是在评估我的 JS 代码文件中的代码..?


更新:一些额外的细节......

Resharper 版本是:

JetBrains ReSharper 7.1.3 Full Edition Build 7.1.3000.2254 on 2013-04-10T16:48:18

Phantom JS 版本是 1.9.1.0

我刚刚意识到我的问题并没有真正的错误——Doh!

TypeError: 'undefined' is not an object (evaluating 'window.MyNamespace.Whatever') in http://localhost:47815/Tests.js (line 26)
TypeError: 'undefined' is not an object (evaluating 'window.MyNamespace.Whatever')
    at  BreadcrumbsSpec.js: line 26
    at  jasmine.js: line 1035
    at  jasmine.js: line 2053
    at  jasmine.js: line 2006
    at  jasmine.js: line 2335
    at  jasmine.js: line 2053
    at  jasmine.js:2043

有趣的是,Tests.js 不是我的文件——我猜那是 R# 的东西。

啊 - Tests.js 是我的规范文件(即本例中的 BreadcrumbsSpec.js)。如果我启动开关选项来告诉 Resharper TestRunner 使用浏览器,我会得到相同的结果(即只定义了窗口)和浏览器中的空白页面......

【问题讨论】:

    标签: javascript visual-studio-2012 jasmine phantomjs resharper-7.1


    【解决方案1】:

    Eureka...问题是 ID-10-T 也称为 PEBCAK

    我没有考虑或研究过 R# 是如何实际运行测试的。因此在 Spec.js 中设置了相对于 my specrunner.html

    的引用路径

    R# 实际上将 Spec 文件作为内联脚本注入到 html 页面中,所以我的相对路径都是错误的。

    我只是将引用路径设置为项目根目录的绝对路径,一切都很好。

    // The Jasmine Test Framework
    /// <reference path="/Scripts/jquery-1.10.1.js"/>
    /// <reference path="/Scripts/tests/lib/jasmine-1.3.1/jasmine.js"/>
    /// <reference path="/Scripts/tests/lib/jasmine-1.3.1/jasmine-html.js"/>
    /// <reference path="/Scripts/tests/lib/jasmine-jquery.js"/>
    /// <reference path="/Scripts/tests/lib/mock-ajax.js"/>
    // Classes to test
    /// <reference path="/Scripts/MyNamespace.Navigation.js"/>
    

    感谢 R# 用户在 Twitter 上与我互动!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-17
      相关资源
      最近更新 更多