0

我目前正在测试响应对象中是否存在属性。但是如果它不存在,那么下一个测试会使整个事情陷入困境,因为 test.done() 永远不会被调用。

foo.execute( function( error, response ) {

  test.ok( typeof response.request != 'undefined',
    "The response should have a request property" );

  test.equal( response.request.method, 'GET',
    "The request header should reflect the method used." );

  test.done();

});

结果是……

失败:未完成的测试(或其设置/拆卸):- PostResourseGetSucceeds

要解决此问题,请确保所有测试都调用 test.done()

我怎样才能让测试仍然报告初始测试?我试过放入 if 语句,但它似乎很麻烦。

foo.execute( function( error, response ) {

  test.ok( typeof response.request != 'undefined',
    "The response should have a request property" );

  if ( typeof response.request != 'undefined' ) {
    test.equal( response.request.method, 'GET',
      "The request header should reflect the method used." );
  }

  test.done();

});
4

1 回答 1

0

我找到了一个简单的解决方案,试着抓住。

foo.execute( function( error, response ) {

  try {

    test.ok( typeof response.request != 'undefined',
      "The response should have a request property" );

    test.equal( response.request.method, 'GET',
      "The request header should reflect the method used." );

  }
  catch(e) {
    test.ok( false, "Not all tests were run." );
  };

  test.done();

});
于 2014-02-06T15:38:05.130 回答