本文介紹了是否有茉莉花匹配器來比較對象的屬性子集的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
我有一個對象可以根據我的被測行為進行擴展,但我想確保原始屬性仍然存在.
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/47884032ad255e8e15144dcd3545c3267795de/一>它甚至適用于嵌套對象,您只需將要部分匹配的每個對象包裝在 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})
}));
});
這篇關于是否有茉莉花匹配器來比較對象的屬性子集的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!