0

我正在努力将 JavaScript 单元测试包含到我正在处理的 ASP.NET 项目中。我已经安装了 Chutzpah 适配器和 Jasmine。在仔细按照此 Ryan Cromwell 视频提供的安装和设置环境的指导后,我遇到了与此帖子类似的错误,即 Chutzpah 和 QUnit的问题,但是我使用的是 Jasmine 而不是 QUnit,他的解决方案似乎没有应用,至少不是以同样的方式。

这是正在测试的 JavaScript (WebApp.Utils.js)。正在测试的函数是使用“显示模块”模式定义的 WebApp.Utils“类”的方法(不确定这是否相关)......

var WebApp = (typeof WebApp === "undefined") ? { } : WebApp ;

$(document).ready(function () {

    WebApp .Utils = (function () {

        var ParseDecimal = function (value, decimalPlaces) {
            /// <summary>Parses a number into a decimal value with the specified number of decimal places</summary>
            /// <param name="value" type="number">Number to parse</param>
            /// <param name="decimalPlaces" type="integer">Number of decimal places to parse to. </param>
            ///
            return parseFloat(Math.round(value * 100) / 100).toFixed(decimalPlaces);
        }

        return {

            ParseDecimal: ParseDecimal
        }

    })();

});

这是编写的测试,使用Jasmine进行测试。文件引用已被验证为正确。事实上,IntelliSense 正在工作,所以我知道它可以看到它。

/// <reference path="../tools/jasmine.js" />
/// <reference path="../shared/WebApp.Utils.js" />
/// 

describe("WebApp.Utils.ParseDecimal", function () {


    it("can ParseDecimal", function () {        
        var result = WebApp.Utils.ParseDecimal(15.451222, 2);
        expect(result).toBe(15.45);
    });
});

这是我在保存文件并运行测试时得到的错误是 Visual Studio。

Test Name:  WebApp.Utils can ParseDecimal
Test FullName:  j:\projects - gary\WebApp\rc1iteration05\rc1iteration05\scripts\tests\test.js::WebApp.utils::can parsedecimal
Test Source:    j:\projects - gary\WebApp\rc1iteration05\rc1iteration05\scripts\tests\test.js : line 7
Test Outcome:   Failed
Test Duration:  0:00:00.003

Result Message: TypeError: 'undefined' is not an object (evaluating 'WebApp.Utils.ParseDecimal') in file:///j:/projects%20-%20gary/WebApp/rc1iteration05/rc1iteration05/scripts/tests/test.js (line 9)
        at file:///j:/projects%20-%20gary/WebApp/rc1iteration05/rc1iteration05/scripts/tests/test.js:9
        at file:///C:/USERS/GSTENSTROM/APPDATA/LOCAL/MICROSOFT/VISUALSTUDIO/11.0/EXTENSIONS/ADQAX1GK.5GB/TestFiles/jasmine/jasmine.js:1064
        at file:///C:/USERS/GSTENSTROM/APPDATA/LOCAL/MICROSOFT/VISUALSTUDIO/11.0/EXTENSIONS/ADQAX1GK.5GB/TestFiles/jasmine/jasmine.js:2096
        at file:///C:/USERS/GSTENSTROM/APPDATA/LOCAL/MICROSOFT/VISUALSTUDIO/11.0/EXTENSIONS/ADQAX1GK.5GB/TestFiles/jasmine/jasmine.js:2049
        at file:///C:/USERS/GSTENSTROM/APPDATA/LOCAL/MICROSOFT/VISUALSTUDIO/11.0/EXTENSIONS/ADQAX1GK.5GB/TestFiles/jasmine/jasmine.js:2378
        at file:///C:/USERS/GSTENSTROM/APPDATA/LOCAL/MICROSOFT/VISUALSTUDIO/11.0/EXTENSIONS/ADQAX1GK.5GB/TestFiles/jasmine/jasmine.js:2096
        at file:///C:/USERS/GSTENSTROM/APPDATA/LOCAL/MICROSOFT/VISUALSTUDIO/11.0/EXTENSIONS/ADQAX1GK.5GB/TestFiles/jasmine/jasmine.js:2049
        at file:///C:/USERS/GSTENSTROM/APPDATA/LOCAL/MICROSOFT/VISUALSTUDIO/11.0/EXTENSIONS/ADQAX1GK.5GB/TestFiles/jasmine/jasmine.js:2523
        at file:///C:/USERS/GSTENSTROM/APPDATA/LOCAL/MICROSOFT/VISUALSTUDIO/11.0/EXTENSIONS/ADQAX1GK.5GB/TestFiles/jasmine/jasmine.js:2096
        at file:///C:/USERS/GSTENSTROM/APPDATA/LOCAL/MICROSOFT/VISUALSTUDIO/11.0/EXTENSIONS/ADQAX1GK.5GB/TestFiles/jasmine/jasmine.js:2049
        at file:///C:/USERS/GSTENSTROM/APPDATA/LOCAL/MICROSOFT/VISUALSTUDIO/11.0/EXTENSIONS/ADQAX1GK.5GB/TestFiles/jasmine/jasmine.js:2145
        at file:///C:/USERS/GSTENSTROM/APPDATA/LOCAL/MICROSOFT/VISUALSTUDIO/11.0/EXTENSIONS/ADQAX1GK.5GB/TestFiles/jasmine/jasmine.js:802
        at startJasmine (phantomjs://webpage.evaluate():15)
        at phantomjs://webpage.evaluate():25
4

1 回答 1

1

事实证明,Chuztpah 不识别根级引用(相对于相对引用)。您在我提供的代码中没有看到的是 WebApps.Utls.js 文件还包含对

/// <reference path="/Scripts/jquery/_references.js" />
/// <reference path="/Scripts/tools/modernizr-2.6.2.js" />
/// <reference path="/Scripts/tools/_references.js" />

这些其他参考文件反过来又提供了与 Knockout、jquery 等工具的链接。一旦我将这些单独的相对引用提供到我的 Jasmine 测试文件的顶部,一切正常。

/// <reference path="../../jquery/jquery-2.0.3.js" />
/// <reference path="../../tools/jasmine.js" />
/// <reference path="../../tools/knockout-2.1.0.debug.js" />
/// <reference path="../../tools/modernizr-2.6.2.js" />
/// <reference path="../../tools/jshashset_src.js" />
/// <reference path="../../tools/jshashtable-2.1.js" />
/// <reference path="../../tools/json2.js" />
/// <reference path="../../Models/ServiceLevelDTO.js" />
/// <reference path="../../shared/WebApp.Utils.js" />
于 2013-11-04T19:18:20.193 回答