gpt4 book ai didi

javascript - 如何使用 jasmine 或 mocha 对接受参数的 javascript 函数进行单元测试

转载 作者:行者123 更新时间:2023-11-29 18:13:02 25 4
gpt4 key购买 nike

我有一个简单的问题。如何对依赖于参数的函数进行单元测试?比如说:

代码:

function a(param) {
if(param > 0)
return param+value;
else
return param;
}

如何在没有参数的情况下对函数 a 进行单元测试?我听说我在 Jasmine 中使用模拟或 spy 。谁能给我举个例子,我真的很困惑。谢谢大家。

编辑:

感谢大卫如此全面的回答。对此,我真的非常感激。这是有关我的问题的更多信息。

这实际上是我真正的问题,我有一个文件

snap-fed.js:

//Code here...

我想按照您向我展示的那样进行全面的单元测试。但我不确定如何使用 Jasmine 或 Mocha 来做到这一点。

比如我如何测试 snap 对象的任何方法?我如何对 snap.eligibility 或 snap.isSnapResourceEligibile 进行单元测试?我在这个问题上卡了2天左右,实在是搞不懂。

它们都接受一个参数信息,该参数提供有关方法正在处理的对象的信息。

这是我真正的问题,但我不知道如何提出。

编辑2:

我根据 David 的模板制作了这个,但它甚至无法运行...

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

});

});

最佳答案

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

});

这个场景期望函数 a 中的变量 value 是一个等于 5 的常量。这个场景可能更复杂,测试看起来会有所不同。如果关于 value 变量有任何逻辑,请在您的问题中显示它,然后我将编辑我的答案。

编辑:问题第二部分的测试样本

因此,如果在移除 Q.fcall 之后资格函数看起来像这样:

eligibility: function (info) {
return {
eligible : snap.isSnapIncomeEligible(info) && snap.isSnapResourceEligible(info),
savings : snap.calcSavings(info)
};
}

然后您可以像这样测试 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);
});

});

我没有用测试覆盖整个 snap 对象。但更多的测试将是相似的,我的代码应该是一个例子,更多的测试以类似的方式构建。

关于javascript - 如何使用 jasmine 或 mocha 对接受参数的 javascript 函数进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25617295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com