简单的TDD Jasmine/Karma测试由于未定义的函数而失败
Simple TDD Jasmine / Karma test failing as undefined function
我对茉莉花/因果报应完全陌生,有一些问题。当我运行一个简单的时,测试运行良好
describe('JavaScript addition operator', function () {
it('adds two numbers together', function () {
expect(1 + 2).toEqual(3);
});
});
测试,它通过了,还可以,但我现在想开始测试我的othe文件中的功能,很自然,我从最难的一个开始,结果失败了。然后,我一路往下看列表/错误,直到我得到了最基本的函数,一个通过输入params将数字四舍五入到小数点的函数。它给了我一个未定义的错误,所以我想我应该把有效的添加测试移到那个文件中,看看我是否很特别,它也不起作用,所以有人能告诉我我做错了什么吗?:)
我在网上搜索了很长一段时间,还没有找到白痴指南。我希望能够通过传递我期望的params来测试我的函数。例如:
describe("round results", function(){
var myFunc = roundresult(a,b);
var a = 99.923232;
var b = 1;
it("rounds the result to dec places", function(){
expect(myFunc(a,b).toEqual(99.9));
});
});
这里是函数:
function roundResult(value, places) {
var multiplier = Math.pow(10, places);
return (Math.round(value * multiplier) / multiplier);
}
错误:
ReferenceError: roundresult is not defined
at null.<anonymous> (http://localhost:9878/base/tests/objectTests.js:98:18)
at jasmine.Env.describe_ (http://localhost:9878/absolute/usr/local/lib/node_modules/karma-jasmine/lib/jasmine.js:884:21)
at jasmine.Env.describe (http://localhost:9878/absolute/usr/local/lib/node_modules/karma-jasmine/lib/jasmine.js:869:15)
at describe (http://localhost:9878/absolute/usr/local/lib/node_modules/karma-jasmine/lib/jasmine.js:629:27)
at http://localhost:9878/base/tests/objectTestTests.js:96:1
非常感谢您的帮助。
在describe
块中,roundresult
应该是roundResult
。
SOLVED:您需要文件的顺序决定了在尝试调用语句时是否已经定义了它。使用plnkr.co可以承载一个包含多个文件的示例。
expect(myFunc(a,b).toEqual(99.9));//不正确,应如下所述
expect().toEqual();
您已经调用了函数并初始化了变量,这也是不正确的:
var myFunc=舍入结果(a,b);
var a=99.923232;
var b=1;
请看一下我是怎么做到的:
//我已经创建了test.js
description("舍入结果",function(){var a=99.923232;
var b=1;var myfunc=roundResult(a,b);
it("rounds the result to dec places", function(){ expect(myfunc).toEqual(99.923232); });
});
//main.js
函数roundResult(值,位置){
var乘数=值*位
收益乘数
}
我希望它能解决你的错误。
谢谢。
- Jquery未定义函数正在停止其他操作
- 在不破坏未定义函数的情况下,对多个视图使用单个js文件
- Uncaught ReferenceError:尝试在Android网络视图中访问时未定义函数
- jQuery文件上传和验证未定义函数
- 引用错误:未定义函数-Firefox
- res.render中出错,未定义函数
- 神秘的未定义函数
- 尝试使用函数和Ajax修改值时未定义函数
- 引用错误:未定义函数
- ReferenceError:未定义函数
- 在 Angular JS 中使用 ng-include 的未定义函数
- 未捕获的引用错误:未定义函数
- j查询工具提示引发错误:未定义函数
- 未捕获的引用错误: 未定义单击世界 -- 未定义函数
- JS:引用错误:未定义函数
- 茉莉花未定义函数
- Javascript 错误:未捕获的引用错误:未定义 [函数]
- 未定义函数
- 尽管 js 文件引用在标头中,但未定义函数
- Javascript 未定义函数错误