Javascript 茉莉花单元测试 - 测试对象的未定义属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4648303/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 13:26:39  来源:igfitidea点击:

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

javascriptunit-testingbddundefinedjasmine

提问by wmitchell

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.

由于我的对象 A 具有顶级属性“BAR”,而 bar 具有值“foo”传递。

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

我想测试我的结构以确认尚未定义属性“NONEXISTINGPROP”。例如

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

谢谢 SO

回答by wmitchell

The answer seems to be ...

答案似乎是...

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

ie remove the name bit

即删除名称位