Unit testing 如何对某个对象进行单元测试!==使用jasmine和check flag?

Unit testing 如何对某个对象进行单元测试!==使用jasmine和check flag?,unit-testing,jasmine,karma-jasmine,angular-test,angular-unit-test,Unit Testing,Jasmine,Karma Jasmine,Angular Test,Angular Unit Test,嘿,我的Angular应用程序中有这个方法,我想进行单元测试: public methodEquip(someBonus: Parameters) { let flag = false; for (const shield of someBonus.items) { if (shield.added.length !== 0 || shield.removed.length !== 0) { flag = true }

嘿,我的Angular应用程序中有这个方法,我想进行单元测试:

public methodEquip(someBonus: Parameters) {
    let flag = false;
    for (const shield of someBonus.items) {
        if (shield.added.length !== 0 || shield.removed.length !== 0) {
            flag = true
        }
        if (flag) {
            return true;
        } else {
            return false;
        }
    }
}
我想用Jasmine进行单元测试。我可以做简单的单元测试,但现在对我来说太多了,我累坏了。我对单元测试非常陌生,不知道如何做:/ 你能帮我吗

我现在只有这个,我不知道如何做它的其余部分:

it('tests methodEquip', () => {
       let flag = false;
       const newMocked = new Parameters;
       component.methodEquip(newMocked);        
});

对于Jasmine,任何
匹配器
都可以通过在调用
匹配器
之前将对
expect
的调用与
not
链接来评估为否定断言

对于基本类型(布尔、数字、字符串等):

对于对象:

expect(actual).not.toEqual(x);
在您的情况下,测试可能如下所示:

it('#methodEquip should not return false when ...', () => {
    const parameters = new Parameters;
    const actual = component.methodEquip(parameters);
    expect(actual).not.toBe(false);
});
由于
boolean
只有两个可能的值,因此只需编写
expect(实际).toBe(真)


我可以添加长度值以检查其他选项吗?这有意义吗?或者这个单一测试应该足够了?您通常会为每个方法编写1-n个单元测试,以确保所有不同的代码分支至少执行一个并产生预期的结果。在这个测试用例中,我遇到了错误“无法读取未定义的属性‘长度’”,我现在正在寻找一些提示
it('#methodEquip should not return false when ...', () => {
    const parameters = new Parameters;
    const actual = component.methodEquip(parameters);
    expect(actual).not.toBe(false);
});