简体   繁体   中英

Qunit test property exists in object

I am using Qunit testing engine in JavaScript and I need to check if a property exists in an object.

Here is my setup:

 var tests = [
    {
        foo: 'foo',
        bar: 'bar',
        expected: {}
    },
    {
        foo: '',
        bar: '',
        expected: //I need to expect foo and bar properties to exist
    }
 ]

 for(var i = 0; i < tests.length; i++) {
    assert.deepEqual(validate_foo(tests[i]), tests[i].expected);
 }

The function validate_foo() either will return an empty object {} on success, or an object with the properties of each error. For example, validate_foo() expectes foo to equal foo and bar to equal bar . So in the second test, the result is:

{
    foo: 'foo does not equal foo',
    bar: 'bar does not equal bar'
}

How do I write a test to accomblish this?

Maybe I have totally missed the point, but assuming you are testing the validate_foo function, isn't this all you need to do?

var tests = [
    {
        foo: 'foo',
        bar: 'bar',
        expected: {}
    },
    {
        foo: '',
        bar: '',
        expected: {foo: 'foo does not equal foo',
                   bar: 'bar does not equal bar'}
    }
 ]

 for(var i = 0; i < tests.length; i++) {
    assert.deepEqual(validate_foo(tests[i]), tests[i].expected);
 }

...and if that's not the test you want then can you clarify what specifically you are trying to test in this case?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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