无法在Visual Studio中使用Chutzpah加载jasmine-jquery fixtures,甚至无法在浏览器中加载

4

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

但是,我无法加载测试fixture(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();
    });
});

测试文件和fixture文件在同一个文件夹中。使用setFixtures操作可以正常工作,但是当我尝试从文件加载HTML时却无法正常运行。最初,我尝试使用存储库中最新版本的jasmine-jquery,但后来退回到了一年前的下载版本1.3.1,因为看起来新版本存在一个bug。以下是我在1.3.1中遇到的错误信息:

测试'jasmine tests::正确复制数据'失败 错误:Fixture无法加载:./testfixture.html(状态:错误,消息:未定义)在file:///C:/Users/db66162/SvnProjects/MvcPrototype/MvcPrototype.Tests/Scripts/jasmine/jasmine-jquery-1.3.1.js(第103行)

当我检查源代码时,它正在进行AJAX调用,但我没有在浏览器中运行。相反,我使用的是Chutzpah,它运行一个无头浏览器(PhantomJS)。当我在带有测试套件的浏览器中运行时,它可以正常工作。

有没有人能够解决这个问题?我需要能够在Visual Studio和TeamCity中自动运行这些测试(这就是为什么我使用Chutzpah的原因)。如果您知道qUnit在我的配置中没有这个问题,我也会考虑使用qUnit测试框架。

3个回答

1

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

"TestHarnessLocationMode": "SettingsFileAdjacent",

chutzpah.json 文件在我的测试应用根目录中。


0

我的问题最终得到了解决。感谢Ian的回复。我能够在TeamCity中使用PhantomJS来通过测试运行器运行测试。我联系了Chutzpah的作者,他部署了更新来解决我在Visual Studio中遇到的问题。现在我可以使用Chutzpah约定来引用库和包含固定装置,在VS中运行Jasmine测试,并在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
        }
    });
}());

0

我遇到了完全相同的问题。据我所知,这与 jasmine-jquery 尝试通过 Ajax 加载 fixtures 有关,当测试通过 file:// URI 方案运行时。

显然,Chrome 不允许这样做(请参见 https://dev59.com/T2035IYBdhLWcg3wW-v0#5469527http://code.google.com/p/chromium/issues/detail?id=40787),其他浏览器的支持可能会有所不同。

编辑

您可以尝试设置一些 PhantomJS 命令行选项,例如 --web-security=false,也许会有所帮助。但是,效果因人而异:我自己还没有尝试过,但我想提一下,以防它有用(或者在任何其他人更多地了解此选项及其是否有帮助的情况下)。

更新

我成功加载了 HTML fixtures,方法是在我的 Jasmine spec 顶部添加一个 /// <reference path="relative/path/to/fixtures" /> 注释。但我仍然无法加载 JSON fixtures。

进一步更新

通过添加/// <reference path="relative/path/to/fixtures" />注释来加载HTML fixtures仅仅是将它们加载到Jasmine测试运行器中,这可能适合您的需求,也可能不适合。它并没有将fixtures加载到jasmine-fixtures元素中,因此您的fixtures在每次测试后都不会被清除。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接