2014-09-25 146 views
0

我有困难理解下面的代码,利用摩卡和薛宝钗在的NodeJS现场测试:功能现场测试采用摩卡和湾仔的NodeJS

suite('Global Tests', function(){ 
     test('page has a valid title', function(){ 
       assert(document.title && document.title.match(/\S/) && 
         document.title.toUpperCase() === 'AHC'); 
     }); 
}); 

什么是混淆是如何代码工作还没有似乎遵循chai documentation断言。其中格式如下:

断言(表达式,消息)

回答

1

这是不使所述第二参数(消息)。这不是无效的,只是使得确定测试失败的原因变得更加困难。如果你想让测试更清晰,你可以将表达式分解为三个断言,并传递一条关于如果验证是否断言的消息。

suite('Global Tests', function(){ 
    test('page has a valid title', function(){ 
    assert(document.title, 'document title is not defined'); 
    assert(document.title.match(/\S/), 'document title does not have a single character'); 
    assert(document.title.toUpperCase() === 'AHC', 'Document title does not equal AHC'); 
    }); 
});  
+0

我已添加assert('foo'!=='bar','foo is not bar');测试消息的工作方式。我把它放在上面的代码的不同部分,不能看到消息“foo不是酒吧”。你能用上面的代码提供一个多重断言的例子吗? – JnL 2014-09-25 04:21:06

+0

以示例更新了答案。如果你想看到这个消息,我建议在第三个断言中改变'AHC'的值。请注意,断言示例'foo'!=='bar'始终为真,因此断言永远不会发生。 – 2014-09-25 04:31:06

+0

我运行了你的代码,我发现浏览器'页面中显示的消息有一个有效的标题'(带有一个检查或X取决于是否通过),而不是与assert语句相关的消息。断言消息是否应该显示在浏览器中? – JnL 2014-09-25 04:38:30