摩卡咖啡相等测试是什么? [英] What are Mocha equal tests?

查看:78
本文介绍了摩卡咖啡相等测试是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Mocha测试Express Node应用程序.我想进行以下测试(比较两个空数组):

I am testing an Express Node app with Mocha. I would like to have the following test (comparing two empty arrays):

assert.equal [], []

通过.但是,摩卡给我以下错误: AssertionError: [] == []

to pass. However, Mocha gives me the following error: AssertionError: [] == []

我应该使用哪种方法比较两个空数组?

Which method should I use in order for comparison of two empty arrays to pass?

推荐答案

问题是数组是JavaScript中的引用类型,因此仅比较引用.而且,当然,如果创建彼此独立的两个不同的空数组,则它们是两个不同的对象,并且具有两个不同的引用.

The problem is that an array is a reference type in JavaScript, and hence only the reference is compared. And, of course, if you create two different empty arrays independent of each other, they are two different objects and have two different references.

这就是测试失败的原因.

That's why the test fails.

您基本上对对象有相同的问题(没有进行深度相等),尽管您通常不关心两个对象是否相同,但是它们的内容是否相同.

You basically have the same issues with objects (no deep-equal is done), although you often are not interested in whether two objects are identical, but whether their contents are the same.

这就是为什么我编写了一个模块来处理此问题的原因: comparejs .除了一些其他优点之外,该模块还通过针对所有(!)类型提供按值比较和按标识比较的方法来解决此问题.我想这就是您需要的.

That's why I wrote a module to handle this: comparejs. This module - besides some other nice things - solves this issue by offering comparison by value and comparison by identity, for all (!) types. I guess that's what you need here.

当您特别要求Mocha的上下文时,我还编写了自己的断言模块,称为 node-assertthat ,内部使用comparejs.副作用是,您获得了更具可读性(更流畅)的语法.代替

As you are especially asking for the context of mocha, I have also written my own assert-module, called node-assertthat, which internally makes use of comparejs. As a side effect, you get a more readable (as more fluent) syntax. Instead of

assert.equal(foo, bar);

你可以写

assert.that(foo, is.equalTo(bar));

也许这可能是您的选择.

Perhaps this may be the way for you to go.

PS:我知道在Stackoverflow上不需要自我宣传,但是在这种情况下,我为自己编写的工具可以解决原始张贴者的问题.因此,请勿将此答案标记为垃圾邮件.

这篇关于摩卡咖啡相等测试是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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