我正在使用 Jasmine.js 编写 JS 单元测试,不确定这种类型的代码是否违反了任何类型的测试原则:
expect(someObject).toNotBe(undefined || null);
与
expect(someObject).toNotBe(undefined);
expect(someObject).toNotBe(null);
尽管null和undefined不同,但出于我测试的目的,我并不(认为我)关心它是哪一个。
我正在使用 Jasmine.js 编写 JS 单元测试,不确定这种类型的代码是否违反了任何类型的测试原则:
expect(someObject).toNotBe(undefined || null);
与
expect(someObject).toNotBe(undefined);
expect(someObject).toNotBe(null);
尽管null和undefined不同,但出于我测试的目的,我并不(认为我)关心它是哪一个。
undefined || null返回null,因为undefined是假的:
> undefined || null
null
您的第一个示例实际上等同于您的第二个示例的第二行,即:
expect(someObject).toNotBe(null);
此外,toNotBe 已弃用:
旧的匹配器
toNotEqual、toNotBe、toNotMatch和toNotContain已被弃用,将在未来的版本中删除。请更改您的规格以分别使用not.toEqual、not.toBe、not.toMatch和not.toContain。
null您可能想用, as来检查相等性(不是身份!)false != null,但是undefined == null:
expect(someObject).not.toEqual(null);
如果someObject, false, 0,[]等也是不可取的,你也可以这样做:
expect(someObject).toBeTruthy();
否则,您应该编写自己的匹配器。