问题描述:

I am working to include JavaScript unit testing into an ASP.NET project that I am working on. I have installed the Chutzpah Adapter and Jasmine. After carefully following the direction provided by this Ryan Cromwell video for installing and setting up the environment, I am encountering a similar error as this post for issues with Chutzpah and QUnit, however I am using Jasmine instead of QUnit and his solution does not appear to apply, at least not in the same way.

Here's the JavaScript being tested (WebApp.Utils.js). The function being tested is method of the WebApp.Utils "class" defined using the "Revealing Module" pattern (not sure if that's relevant or not) ...

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

}

})();

});

Here is the test written, using Jasmine to test it. File references have been verified as being correct. In fact, IntelliSense is working so I know it sees it.

/// <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);

});

});

And here is the error I get is Visual Studio when I save the file and run the test.

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

网友答案:

As it turns out Chuztpah does not recognize root level references (vs. relative references). What you did not see in the code I provided was that the WebApps.Utls.js file also contained a reference to

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

These other reference files, in turn provide linkage to tools such as Knockout, jquery, etc. Once I provided these individual relative references to the top of my Jasmine test file everything worked fine.

/// <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" />
相关阅读:
Top