我正在努力将 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