是否有茉莉花匹配器来比较其属性子集上的对象 [英] Is there a jasmine matcher to compare objects on subsets of their properties
本文介绍了是否有茉莉花匹配器来比较其属性子集上的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个可以根据我的测试行为进行扩展的对象,但我想确保原始属性仍然存在。
I have an object that may be extended along my behavior under test, but I want to make sure that the original properties are still there.
var example = {'foo':'bar', 'bar':'baz'}
var result = extendingPipeline(example)
// {'foo':'bar', 'bar':'baz', 'extension': Function}
expect(result).toEqual(example) //fails miserably
我希望有一个匹配器,在这种情况下会传递:
I'd like to have a matcher that would pass in this case, along the lines of:
expect(result).toInclude(example)
我知道我可以编写自定义匹配器,但在我看来,这是一个常见的问题,解决方案应该已经存在。我应该在哪里寻找它?
I know that I can write a custom matcher, but it seems to me that this is such a common problem that a solution should be out there already. Where should I look for it?
推荐答案
Jasmine 2.0
Jasmine 2.0
expect(result).toEqual(jasmine.objectContaining(example))
自此修复: https://github.com/pivotal/jasmine/commit/47884032ad255e8e15144dcd3545c3267795dee0
它甚至适用于嵌套对象,你只需要在中包含你想要匹配的每个对象jasmine.objectContaining()
简单示例:
it('can match nested partial objects', function ()
{
var joc = jasmine.objectContaining;
expect({
a: {x: 1, y: 2},
b: 'hi'
}).toEqual(joc({
a: joc({ x: 1})
}));
});
这篇关于是否有茉莉花匹配器来比较其属性子集上的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文