如何使用 jasmine 或 mocha 对接受参数的 javascript 函数进行单元测试
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25617295/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to unit test javascript function which takes in a parameter using jasmine or mocha
提问by mosawi
I have a simple question. How do I unit test a function which is dependent on a parameter? Like say for example:
我有一个简单的问题。如何对依赖于参数的函数进行单元测试?比如说:
Code:
代码:
function a(param) {
if(param > 0)
return param+value;
else
return param;
}
How do I unit test function a without having param? I hear this I use mocks or spies in jasmine. Can someone show me an example, I am really confused. Thank you all in advance.
如何在没有参数的情况下对函数 a 进行单元测试?我听说我在茉莉花中使用了模拟或间谍。有人可以给我举个例子,我真的很困惑。谢谢大家。
Edit:
编辑:
Thank you for such a conprehensive answer David. I really appreciate it. Here is more information about my question.
感谢大卫如此全面的回答。对此,我真的非常感激。这是有关我的问题的更多信息。
This is in fact my true question, I have a file
这实际上是我真正的问题,我有一个文件
snap-fed.js:
snap-fed.js:
//Code here...
Which I would like to unit test comprehensively as you showed me. But I am unsure as to how to do this using jasmine or mocha.
正如您向我展示的那样,我想对其进行全面的单元测试。但我不确定如何使用茉莉花或摩卡来做到这一点。
Like how could I test any of the methods of the snap object? How could I unit test snap.eligibility or snap.isSnapResourceEligibile? I have been stuck on this issue for about 2 days, I really don't understand.
比如我如何测试 snap 对象的任何方法?我如何对 snap.eligibility 或 snap.isSnapResourceEligibile 进行单元测试?我在这个问题上被困了大约2天,我真的不明白。
They all take in a parameter info which provides info about the object being worked on by the methods.
它们都接受一个参数信息,该信息提供有关方法正在处理的对象的信息。
This was my true question but I did not know how to ask it.
这是我真正的问题,但我不知道该怎么问。
Edit 2:
编辑2:
Based on David's template I made this, but it doesn't even run...
基于大卫的模板,我做了这个,但它甚至没有运行......
snap-fed.spec.js:
snap-fed.spec.js:
describe("snap-fed", function() {
describe("Properties", function() {
it("should exist", function() {
expect(Allowance).not.toBeUndefined();
expect(AllowanceAdditional).not.toBeUndefined();
expect(MaxAllowanceHouseholdSize).not.toBeUndefined();
});
it("should contain correct values", function() {
expect(Allowance).toEqual([189, 347, 497, 632, 750, 900, 995, 1137]);
expect(AllowanceAdditional).toBe(142);
expect(MaxAllowanceHouseholdSize).toBe(Allowance.length);
});
});
describe("Functions", functions(){
it("should return the expected result", function() {
expect(snap.isSnapResourceEligible(info)).toBeTruthy();
});
//Put more test cases for the various methods of snap
});
});
回答by David Bohunek
The test could look like this in Jasmine:
Jasmine 中的测试可能如下所示:
describe("Function a", function() {
it("is defined", function() {
expect(a).not.toBeUndefined();
});
it("should return expected result for a positive parameter", function() {
var result = a(19);
expect(result).toEqual(24);
});
it("should return expected result for a negative parameter", function() {
var result = a(-1);
expect(result).toEqual(-1);
});
it("should return expected result for parameter zero", function() {
var result = a(0);
expect(result).toEqual(5);
});
});
This scenario is expecting the variable value
inside function a
to be a constant equal to 5. The scenario can be more complex and the tests would look differently. If there is any logic about the value
variable, please show it in your question, I will then edit my answer.
这个场景期望value
函数内部的变量a
是一个等于 5 的常量。这个场景可能更复杂,测试看起来会有所不同。如果该value
变量有任何逻辑,请将其显示在您的问题中,然后我将编辑我的答案。
EDIT: test sample for the second part of your question
编辑:问题第二部分的测试样本
So if the eligibility function looked like this after removing the Q.fcall
:
因此,如果删除 后资格函数看起来像这样Q.fcall
:
eligibility: function (info) {
return {
eligible : snap.isSnapIncomeEligible(info) && snap.isSnapResourceEligible(info),
savings : snap.calcSavings(info)
};
}
Then you could test the snap object like this for example:
然后你可以像这样测试 snap 对象,例如:
describe("Snap", function() {
var snap = ... //get the snap object here...
var disabledMemberInfo = {
disabledMember: true,
age: 50,
fpl: 1.2,
householdSize: 10,
moneyBalance: 4000
};
it("is defined", function() {
expect(snap).not.toBeUndefined();
});
it("has eligibility defined", function() {
expect(snap.eligibility).not.toBeUndefined();
});
it("return positive eligibility for disabled member with fpl < 1.2 and money balance 4000", function() {
var result = snap.eligibility(disabledMemberInfo);
expect(result).not.toBeUndefined();
expect(result.eligible).not.toBeUndefined();
expect(result.savings).not.toBeUndefined();
expect(result.eligible).toEqual(true);
});
});
I didn't cover the whole snap object with tests. But more test will be similar, my code should be an example and more tests built in a similar way.
我没有用测试覆盖整个 snap 对象。但是更多的测试将是相似的,我的代码应该是一个示例,更多的测试以类似的方式构建。