Chai断言测试对象结构是否至少包含其他对象结构 [英] Chai assertion testing whether object structure contains at least other object structure

查看:175
本文介绍了Chai断言测试对象结构是否至少包含其他对象结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Mocha进行单元测试,并使用Chai进行断言.

I'm using Mocha for unit testing and Chai for assertions.

我想找到一种易于使用的解决方案来检查对象是否具有比较对象中定义的结构和属性.但是我不需要对象完全相等.被测对象应该至少包含我测试对象中的所有属性,但它可能还包含当时尚未被测的其他属性.

I'd like to find an easy to use solution to check if an object has the structure and properties as defined in my comparison object. But I don't need the objects to be completely equal. The subject under test should contain at least all the properties in my test object, but it might also contain other properties which are not under test at that moment.

因此,我想测试一个单元,以检查它返回的对象是否至少具有一个名为"foo"的属性,该属性本身是一个至少包含值为10的属性"bar"的对象.该预期结果可用于测试:

So, I want to test a unit to check if the object it returns has at least a property named 'foo', which itself is an object that at least contains the property 'bar' with value 10. So, I have this expected outcome to test against:

var expected = {foo: {bar: 10}};

我打电话给我的单元,并将测试主题放在变量sut中:

I call my unit and have my test subject in a variable sut:

var sut = MyUnit.myFunction();

所以对于各种sut,我希望得到以下结果:

So for various suts, I expect these outcomes:

// Success. Exact match
{foo: {bar: 10}}

// Fail. Structure is ok, but property value is wrong.
{foo: {bar: 11}}

// Fail. property bar is missing.
{foo: {qux: 20}}

// Success. All properties match. Extra properties (baz) in sut are ignored:
{baz: 'a', foo: {bar: 10, baz: 20}}

然后我想以一种方便的方式对其进行比较.我可以单独测试所有属性,也可以将其拆分为多个测试,但我希望可以做类似的事情

And then I want to compare it in a convenient way. I could test all properties individually or split it in a number of tests, but I hoped I could do something like

sut.should.deep.contain.all(expected);

但是,即使对象完全相同,我也得到了以下令人惊讶的结果:

However, I got the following surprising result, even if the objects are exactly the same:

AssertionError:预期{foo:{bar:10}}的属性'foo'为{bar:10},但得到{bar:10}

AssertionError: expected { foo: { bar: 10 } } to have a property 'foo' of { bar: 10 }, but got { bar: 10 }

我当然尝试过这个,还有其他几个变体.如果对象包含额外的属性,则最简单的相等性测试将无法正常工作.

Of course I tried this, and a couple of other variations. The simplest testing for equality, doesn't work if the object contains extra properties.

sut.should.eql(expected);

AssertionError:预期{foo:{bar:10,qux:20}}等于{foo:{bar:10}}

AssertionError: expected { foo: { bar: 10, qux: 20 } } to deeply equal { foo: { bar: 10 } }

我已经测试了havecontains的其他组合以及deepanyall,但是没有一个满足我的愿望.

I've tested other combinations of have and contains together with deep, any or all, but none satisfied my wish.

我发现重复的问题"柴深包含对嵌套对象的断言",但唯一(被否决)的答案没有任何意义.它会调用deep.eql,这是多余的,而且上面的代码只是无效,因为它会测试严格的相等性.

I found the duplicate question "Chai deep contains assertion on nested objects", but the only (downvoted) answer doesn't make sense. It calls deep.eql which is redundant, and above that it just doesn't work, because it tests for strict equality.

我确实知道我可以分别测试所有属性,但是如果我能以一种可读的,单语句的方式测试一个对象是否是另一个对象的子集,那将是一件好事.

I do know I can test all properties separately, but I would be nice to have a readable, single-statement way to test if one object is 'a subset of' the other.

更新: 我最终对Chai使用了浅深度等于插件.

Update: I ended up using the Shallow Deep Equal plugin for Chai.

推荐答案

Chai有几个插件可以解决此问题.

There are a couple of plugins for Chai which can solve this issue.

chai-subset

对于Chai,有一个子集插件应该可以做到.

There is this subset plugin for Chai, that should be able to do this.

我在浏览器中使用Mocha,但是尽管它应该与浏览器兼容,但我还没有使该插件正常工作.

I'm using Mocha in the browser, but although it should be browser compatible, I haven't got this plugin working yet.

无论如何,该库包含对该问题的通用答案,并且在包含该问题之后,以下行应起作用:

Anyway, this library contains the generic answer to the question, and after including it, the following line should work:

sut.should.containSubset(expected);

chai-shallow-deep-equal

chai-subset似乎缺少在浏览器中运行它所需的版本,因此我继续寻找插件.我发现的另一个是 chai-shallow-deep-equal .

chai-subset seemed to lack the version that is required to run it in the browser, so I went on looking for plugins. Another one that I found is chai-shallow-deep-equal.

此插件也可以在浏览器中正常使用. 从Git下载并在<插件页面,结果:

This plugin can be used fine in the browser too. Had it up and running in seconds after downloading it from Git and using the description on the plugin page, resulting in:

sut.should.shallowDeepEqual(expected);

现在它可以很好地忽略sut中的其他属性,但是当expected中的属性缺失或不同时,它也会给出很好的断言.您会收到这样的消息:

It will now nicely ignore extra properties in sut, but it will also give nice assertions when properties as missing or different in expected. You'll get a message like this:

AssertionError:预期为"2",但在路径"/foo/qux"处为"20".

AssertionError: Expected to have "2" but got "20" at path "/foo/qux".

但是,它不会显示所有断言.如果对象中有两个错误,则只会得到一个(第一个)断言错误.对我来说,这不是真正的问题,但可能会造成混淆,因为它看起来像是您所做的修复,已经在引入了一个新问题.

It doesn't show all assertions, though. If you have two errors in the object, you'll only get one (the first) assertion error. To me that's not really an issue, but it could be confusing, because it might look like a fix you made introduced a new issue while it was already there.

chai-fuzzy

我没有尝试过 chai-fuzzy ,(

I've not tried chai-fuzzy, (GitHub) myself yet, but it seems it can solve the same problem too and its repository also contains the browser compatible version of the plug-in. However, it also requires yet another library, Underscore, which seems a bit overkill for the purpose. It's syntax would look like this:

sut.should.be.like(expected);

这篇关于Chai断言测试对象结构是否至少包含其他对象结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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