4

I'm prototyping a MVC.NET 4.0 application and am defining our Javascript test configuration. I managed to get Jasmine working in VS2012 with the Chutzpah extensions, and I am able to run pure Javascript tests successfully.

However, I am unable to load test fixture (DOM) code and access it from my tests.

Here is the code I'm attempting to run:

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();
    });
});

The fixture is in the same folder as the test file. The setFixtures operation works, but when I attempt to load the HTML from a file, it doesn't. Initially, I tried to use the most recent version of jasmine-jquery from the repository, but then fell back to the over 1 year old download version 1.3.1 because it looked like there was a bug in the newer one. Here is the message I get with 1.3.1:

Test 'jasmine tests::Copies data correctly' failed Error: Fixture could not be loaded: ./testfixture.html (status: error, message: undefined) in file:///C:/Users/db66162/SvnProjects/MvcPrototype/MvcPrototype.Tests/Scripts/jasmine/jasmine-jquery-1.3.1.js (line 103)

When I examine the source, it is doing an AJAX call, yet I'm not running in a browser. Instead, I'm using Chutzpah, which runs a headless browser (PhantomJS). When I run this in the browser with a test harness, it does work.

Is there someone out there who has a solution to this problem? I need to be able to run these tests automatically both in Visual Studio and TeamCity (which is why I am using Chutzpah). I am open to solutions that include using another test runner in place of Chutzpah. I am also going to evaluate the qUnit testing framework in this effort, so if you know that qUnit doesn't have this problem in my configuration, I will find that useful.

4

3 回答 3

1

我通过将以下设置添加到 chutzpah.json 来解决此问题:

"TestHarnessLocationMode": "SettingsFileAdjacent",

chutzpah.json 在我的测试应用程序根目录中

于 2014-04-14T07:24:17.030 回答
0

我有完全相同的问题。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:我自己还没有尝试过,但我想我会提到它以防它有帮助(或者如果其他人知道更多关于这个选项以及它是否会有帮助)。

更新

通过添加一个/// <reference path="relative/path/to/fixtures" />通过在我的 Jasmine 规范顶部添加注释,但我仍然无法加载 JSON 固定装置。

进一步更新

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

于 2013-01-29T10:55:42.047 回答
0

我最终解决了我的问题。谢谢伊恩的回复。我可以在 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
        }
    });
}());
于 2013-04-22T22:46:27.893 回答