预期[]为[] Jasmine,如何检查空数组 [英] Expected [ ] to be [ ] Jasmine, how to check empty array

查看:73
本文介绍了预期[]为[] Jasmine,如何检查空数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试检查空数组时出错。我尝试使用:

Getting error while trying to check for empty array. I tried using:

案例1:初始化为数组

expect(fixture.componentInstance.dataSource).toBe([]);

案例2:初始化为数组

let expectedAry = new Array;
expect(fixture.componentInstance.dataSource).toBe(expectedAry);

两种情况都有相同的错误:

Both the case have the same error:

Expected [  ] to be [  ].

阵列也可以通过长度检查,以下工作正常

Arrays can also be checked by their length, the following works fine

expect(fixture.componentInstance.dataSource.length).toEqual(0); 

0长度是一个选项,但不确定这是否是检查阵列是否正确的正确方法空。我们有更好的选择来检查数组是否为空吗?

0 length is an option, but not sure if that is the right way to check whether an array is empty. Do we have a better option for checking whether an array is empty?

推荐答案

toBe 是一个明确的参考检查。

toBe is an explicit reference check.

expect([1])。toBe([1])将失败,因为引用不同。

expect([1]).toBe([1]) will fail because the references are different.

你应该使用 toEqual ,它有一些智能来检查数组内容而不是仅仅进行参考比较。

You should use toEqual, which has some smarts to check the array contents as opposed to just doing a reference comparison.

这篇关于预期[]为[] Jasmine,如何检查空数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆