使用toBe而不是toEqual属性检查的Jest objectContaining方法?

时间:2018-05-20 19:44:09

标签: javascript unit-testing testing jestjs

是否有类似[SERIAL]的方法使用<owl:Class rdf:about="http://www.co-ode.org/ontologies/pizza/pizza.owl#Country"> <owl:equivalentClass> <owl:Class> <owl:intersectionOf rdf:parseType="Collection"> <rdf:Description rdf:about="http://www.co-ode.org/ontologies/pizza/pizza.owl#DomainConcept"/> <owl:Class> <owl:oneOf rdf:parseType="Collection"> <rdf:Description rdf:about="http://www.co-ode.org/ontologies/pizza/pizza.owl#America"/> <rdf:Description rdf:about="http://www.co-ode.org/ontologies/pizza/pizza.owl#England"/> <rdf:Description rdf:about="http://www.co-ode.org/ontologies/pizza/pizza.owl#France"/> <rdf:Description rdf:about="http://www.co-ode.org/ontologies/pizza/pizza.owl#Germany"/> <rdf:Description rdf:about="http://www.co-ode.org/ontologies/pizza/pizza.owl#Italy"/> </owl:oneOf> </owl:Class> </owl:intersectionOf> </owl:Class> </owl:equivalentClass> <rdfs:comment xml:lang="en">A class that is equivalent to the set of individuals that are described in the enumeration - ie Countries can only be either America, England, France, Germany or Italy and nothing else. Note that these individuals have been asserted to be allDifferent from each other.</rdfs:comment> <rdfs:label xml:lang="en">Country</rdfs:label> <rdfs:label xml:lang="pt">Pais</rdfs:label> <skos:prefLabel xml:lang="en">Country</skos:prefLabel> </owl:Class> 代替objectContaining属性检查?

以下设计的测试用例(通过)突出了该问题。我想声明传递给toBe的对象的toEqual属性实际上是button,因此对于类似的对象foo应该失败。

button1

或者,如何重写此测试用例以支持我所需的断言?

1 个答案:

答案 0 :(得分:0)

我认为在测试中比较输出的方式会导致它通过,因为参数实际上被读取为:

{ "button": <button /> }

而不是文档元素的实例。

在创建过程中,您始终可以为每个按钮分配唯一的ID,以使断言成为可能。

test('foo', () => {
    const button1 = document.createElement('button');
    const button2 = document.createElement('button');
    button1.id = 'button1';
    button2.id = 'button2';
    const foo = jest.fn();
    foo({button: button1});
    expect(foo).toBeCalledWith({button: button1}); // Will pass
    expect(foo).toBeCalledWith({button: button2}); // Will fail
});

如果你想使用toBe()严格的相等性检查,我不知道有一个类似于Jest的Object Contains方法,但你可以询问Jest提供的参数:

expect(foo.mock.calls[0][0].button).toBe(button1);