2016-04-27 167 views
3

我想用邮递员来测试一个API,我正在建立与快递,其中我正在建立一套测试。这里有一个简单的例子:工作为什么测试失败

tests["Status code is 200"] = responseCode.code === 200; 

// Check that we got the expected board 
var expected = { _id: 11, name: "board A" }; 
tests["Board retrieved"] = JSON.stringify(expected) === responseBody; 

但是我发现,当我的测试可能会失败,他们没有告诉我任何有用的东西:

enter image description here

是否有不同的方式来验证结果,这会让我知道任何人都知道的传统测试跑步者的预期/实际值?到目前为止我唯一能想到的就是将信息嵌入测试名称 - 这感觉有点混乱。

回答

2

一个可能的方法可以包裹分配来添加更多的功能:

/** This function adds information in the label only if the validations returns false */ 
tests.assertEquals= function (expected, actual, label) { 
    if (expected!==actual) { 
    this[label + ': Expected "' +expected + '" but got "'+ actual +'"']=false; 

    } else { 
    this[label]=true; 
    } 
} 

tests.assertEquals(JSON.stringify(expected), responseBody,"Board retrieved"); 

正如你所说的,它可能是一个cludge,但你只有这个信息,如果有必要和使用方法保持它'隐藏'使它更清洁,更易于阅读。另一个选择是使用console.log来显示额外的信息,而不是将其添加到标签中,但它只是一个品味问题

+0

感谢Pablo--这是我最近想到的。不幸的是,控制台输出没有出现在我看到的任何地方。 – Ian