简体   繁体   English

比较两个对象QUnit Javascript

[英]Comparing Two Objects QUnit Javascript

I need to compare the properties of two objects and the property type, but not the values just the type. 我需要比较两个对象的属性和属性类型,而不只是值的类型。

So I have var a = {key1 : [], key2: { key3 : '' } } 所以我有var a = {key1 : [], key2: { key3 : '' } }

And I want to compare that to another obejct I get back from a web service call. 我想将其与我从Web服务调用中得到的另一个对象进行比较。

In this case, the response is equal to {key1 : '', key2: { key3 : '' }, key4: 1 } 在这种情况下, response等于{key1 : '', key2: { key3 : '' }, key4: 1 }

I try to do propEqual() 我尝试做propEqual()

 assert.propEqual(response, a, "They are the same!");

This does the testing of the properties I believe, but it is also testing the value of the properties. 我相信这是对特性的测试,但同时也在测试特性的值。 I do not care about the value, I just want to test the overall structure and type. 我不在乎值,我只想测试整体结构和类型。

So giving the above data examples, the test should throw 2 errors. 因此,给出上述数据示例,测试应抛出2个错误。 One would be that, the key1 in the response is a string and I was expecting an array and the other would be that response has a key that is not expected ( key4 ). 一种可能是, responsekey1是一个字符串 ,我正在期待一个数组 ,另一种可能是, response包含了一个不期望的键( key4 )。

Is this possible? 这可能吗? Thanks!! 谢谢!!

You'll need to use your own logic to test what you are looking for. 您将需要使用自己的逻辑来测试所需的内容。 There are pretty much two things to test - types matching, and the number of properties in the response needing to match your object. 有两件事要测试-类型匹配和响应中需要匹配对象的属性数量。 I defined two functions, testTypesEqual (returns true if types match) and testPropertiesMatch (returns true if response has the same properties as your object). 我定义了两个函数, testTypesEqual (如果类型匹配, testPropertiesMatch返回true)和testPropertiesMatch (如果response与对象具有相同的属性,则返回true)。 You'll need to use these (or a variation of these depending on your exact needs) in your tests. 您将需要在测试中使用这些(或根据您的实际需求而定)。 A full example can be found here http://jsfiddle.net/17sb921s/ . 完整的示例可以在http://jsfiddle.net/17sb921s/中找到。

//Tests that the response object contains the same properties 
function testPropertiesMatch(yours, response){
    //If property count doesn't match, test failed
    if(Object.keys(yours).length !== Object.keys(response).length){
        return false;
    }

    //Loop through each property in your obj, and make sure
    //the resposne also has it.
    for(var prop in yours){
        if(!response.hasOwnProperty(prop)){
            //fail if response is missing a property found in your object
            return false;
        }
    }

    return true;
}

//Test that property types are equal
function testTypesEqual(yours, response){
    return typeof(yours) === typeof(response)
}

You'll have to write one assert.ok per property you want to check for type mismatches. 您必须为每个要检查类型不匹配的属性编写一个assert.ok Finally, you'll have a single assert.ok checking that the properties in the response match those in your object. 最后,您将只有一个assert.ok检查response中的属性是否与对象中的属性匹配。

Example: 例:

//fails for key1
assert.ok(testTypesEqual(a.key1, response.key1), "Will fail - key1 property types do not match");

//fails - response contains additional property
assert.ok(testPropertiesMatch(a, response), "Additional Properties - Fail due to additional prop in Response");

Obviously now I have introduced new & non-trivial logic into your unit tests, the sole purpose of this answer is to show you how to do it, not to advise you to take complicated logic from a stranger and stick that all over your unit tests :). 显然,现在我已经在您的单元测试中引入了新的,非平凡的逻辑,此答案的唯一目的是向您展示如何做,而不是建议您从陌生人那里学习复杂的逻辑,并坚持在整个单元测试中坚持下去:)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM