【问题标题】:Unable to get jasmine-jquery fixtures to load in Visual Studio with Chutzpah, or even in browser无法使用 Chutzpah 在 Visual Studio 中加载 jasmine-jquery 固定装置,甚至无法在浏览器中加载
【发布时间】:2012-11-25 23:37:24
【问题描述】:

我正在制作一个 MVC.NET 4.0 应用程序的原型并定义我们的 Javascript 测试配置。我设法让 Jasmine 使用 Chutzpah 扩展在 VS2012 中工作,并且我能够成功运行纯 Javascript 测试。

但是,我无法加载测试夹具 (DOM) 代码并从我的测试中访问它。

这是我尝试运行的代码:

test.js

/// various reference paths...

jasmine.getFixtures().fixturesPath = "./";

describe("jasmine tests:", function () {
    it("Copies data correctly", function () {
        loadFixtures('testfixture.html');
        //setFixtures('<div id="wrapper"><div></div></div>');
        var widget = $("#wrapper");
        expect(widget).toExist();
    });
});

夹具与测试文件位于同一文件夹中。 setFixtures 操作有效,但是当我尝试从文件加载 HTML 时,它没有。最初,我尝试使用存储库中最新版本的 jasmine-jquery,但后来又退回到 1 年多前的下载版本 1.3.1,因为看起来新版本中存在错误。这是我在 1.3.1 中得到的信息:

测试 'jasmine tests::Copies data correct' 失败 错误:无法加载夹具:./testfixture.html(状态:错误,消息:未定义)在 file:///C:/Users/db66162/SvnProjects/MvcPrototype/MvcPrototype.Tests/Scripts/jasmine/jasmine-jquery -1.3.1.js(第 103 行)

当我检查源代码时,它正在执行 AJAX 调用,但我没有在浏览器中运行。相反,我使用的是运行无头浏览器 (PhantomJS) 的 Chutzpah。当我使用测试工具在浏览器中运行它时,它确实有效。

有没有人可以解决这个问题?我需要能够在 Visual Studio 和 TeamCity 中自动运行这些测试(这就是我使用 Chutzpah 的原因)。我对包括使用另一个测试运行器代替 Chutzpah 的解决方案持开放态度。我还将在这项工作中评估 qUnit 测试框架,所以如果您知道 qUnit 在我的配置中没有这个问题,我会发现它很有用。

【问题讨论】:

    标签: visual-studio jasmine jasmine-jquery chutzpah


    【解决方案1】:

    我通过将以下设置添加到 chutzpah.json 解决了这个问题:

    "TestHarnessLocationMode": "SettingsFileAdjacent",
    

    chutzpah.json 在我的测试应用根目录中的位置

    【讨论】:

      【解决方案2】:

      我最终解决了我的问题。谢谢伊恩的回复。我可以在 TeamCity 中使用 PhantomJS 通过测试运行器运行测试。我联系了 Chutzpah 的作者,他为他的产品部署了一个更新,解决了我在 Visual Studio 中的问题。我现在可以使用 Chutzpah 约定运行 Jasmine 测试以在 VS 中引用库和包含固定装置,并使用 TeamCity 中的 PhantomJS 运行程序来使用测试运行程序 (html)。

      我在 TeamCity 上的解决方案是运行一个启动测试的批处理文件。所以,批次:

      @echo off
      REM -- Uses the PhantomJS headless browser packaged with Chutzpah to run 
      REM -- Jasmine tests.  Does not use Chutzpah.
      setlocal
      set path=..\packages\Chutzpah.2.2.1\tools;%path%;
      echo ##teamcity[message text='Starting Jasmine Tests']
      phantomjs.exe phantom.run.js %1
      echo ##teamcity[message text='Finished Jasmine Tests']
      

      还有 Javascript (phantom.run.js):

      // This code lifted from https://gist.github.com/3497509.
      // It takes the test harness HTML file URL as the parameter.  It launches PhantomJS,
      // and waits a specific amount of time before exit.  Tests must complete before that 
      // timer ends.  
      (function () {
          "use strict";
          var system = require("system");
          var url = system.args[1];
      
          phantom.viewportSize = {width: 800, height: 600};
      
          console.log("Opening " + url);
      
          var page = new WebPage();
      
          // This is required because PhantomJS sandboxes the website and it does not
          // show up the console messages form that page by default
          page.onConsoleMessage = function (msg) {
              console.log(msg);
      
              // Exit as soon as the last test finishes.
              if (msg && msg.indexOf("Dixi.") !== -1) {
                  phantom.exit();
              }
          };
      
          page.open(url, function (status) {
              if (status !== 'success') {
                  console.log('Unable to load the address!');
                  phantom.exit(-1);
              } else {
                  // Timeout - kill PhantomJS if still not done after 2 minutes.
                  window.setTimeout(function () {
                      phantom.exit();
                  }, 10 * 1000); // NB: use accurately, tune up referring to your needs
              }
          });
      }());
      

      【讨论】:

        【解决方案3】:

        我遇到了完全相同的问题。 AFAIK 与 jasmine-jquery 在通过 file:// URI 方案运行测试时尝试通过 Ajax 加载固定装置有关。

        显然 Chrome 不允许这样做(请参阅 https://stackoverflow.com/a/5469527/1904http://code.google.com/p/chromium/issues/detail?id=40787),并且其他浏览器的支持可能会有所不同。

        编辑

        可能通过尝试设置一些 PhantomJS 命令行选项(例如 --web-security=false)来获得一些乐趣。虽然 YMMV:我自己还没有尝试过,但我想我会提到它以防万一它有帮助(或者如果其他人知道更多关于这个选项以及它是否会有帮助)。

        更新

        确实通过在我的 Jasmine 规范顶部添加 /// &lt;reference path="relative/path/to/fixtures" /&gt; 注释来设法加载 HTML 固定装置。但我仍然无法加载 JSON 固定装置。

        进一步更新

        通过添加/// &lt;reference path="relative/path/to/fixtures" /&gt; 注释来加载 HTML 固定装置只会将您的 HTML 固定装置加载到 Jasmine 测试运行器,这可能适合也可能不适合您的需求。它不会将夹具加载到jasmine-fixtures 元素中,因此您的夹具在每次测试后都不会被清理。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-10-07
          • 2013-12-11
          • 2020-09-27
          • 1970-01-01
          • 1970-01-01
          • 2013-09-12
          相关资源
          最近更新 更多