jasmine unit testing - testing for an undefined property of an object

无人久伴 提交于 2019-12-21 06:48:08

问题


I have the following statement

expect(A.["BAR"].name).toEqual("foo"); 

which due to the fact my object A has the top level property "BAR" and bar has the value "foo" passes.

I'd like to test my structure to confirm a property "NONEXISTINGPROP" has not be defined. e.g.

expect(A.["NONEXISTINGPROP"].name).not.toBeDefined(); 

However I seem to get

  "TypeError: A.[NONEXISTINGPROP] is undefined" 

in the jasmine test runner this is exactly what I want to confirm. Any idea why Jasmine is crying. I was hoping for it to pass this.

Thanks SO


回答1:


The answer seems to be ...

expect(A.NONEXISTINGPROP).not.toBeDefined(); 

ie remove the name bit



来源:https://stackoverflow.com/questions/4648303/jasmine-unit-testing-testing-for-an-undefined-property-of-an-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!