2015-04-06 71 views
0

我有一个测试套件,是围绕包含〜350测试的JavaScript应用程序代码构建的。我的测试通过从节点命令行运行grunt test来运行。每次测试运行时,我的测试的不同数量都会在停止之前运行。它很少完成我所有的测试。有没有人有过这种事情发生过?有什么想法可能会导致它?运行单元测试的变量成功 - Node,Mocha,Chai,Karma,Grunt

当我运行测试时,我通常会从270 - 350测试完成时得到一个看似随机的数字。测试结束后,我不会收到任何错误或任何信息。它最终说法是这样的:

Finished in 1.332 secs/0.823 secs 

Summary: 
√ 311 test completed 

这里是一个很少被运行的测试的一个示例:

describe('util.js', function() { 
    it('should create the global SeniorHomes object', function() { 
     expect(sh).to.be.an('object'); 
    }); 
    describe('sh.cookies.create', function() { 
     sh.cookies.create('name', 'value', 10); 
     it('should create a cookie', function() { 
      var cookies = document.cookie.split('; '), 
       cookie; 
      $.each(cookies, function(i, item) { 
       if (item.split('=')[0] === 'name') { 
        cookie = { 
         name: item.split('=')[0], 
         value: item.split('=')[1] 
        }; 
       } 
      }); 
      expect(cookie.name).to.equal('name'); 
      expect(cookie.value).to.equal('value'); 
     }); 
    }); 
    describe('sh.cookies.read', function() { 
     sh.cookies.create('name2', 'value2', 10); 
     var cookie = sh.cookies.read('name2'); 
     it('should read the value of a cookie', function() { 
      expect(cookie).to.equal('value2'); 
     }); 
    }); 
    describe('sh.cookies.erase', function() { 
     sh.cookies.create('name3', 'value3', 10); 
     sh.cookies.erase('name3'); 
     it('should delete a cookie', function() { 
      expect(sh.cookies.read('name3')).to.equal(null); 
     }); 
    }); 
    describe('sh.format_time', function() { 
     var date1 = new Date('01/31/2005 10:00'), 
      date2 = new Date('01/31/2005 15:59'), 
      output1 = sh.format_time(date1), 
      output2 = sh.format_time(date2); 
     it('should output a formatted time string', function() { 
      expect(output1).to.equal('10:00 am'); 
      expect(output2).to.equal('3:59 pm'); 
     }); 
    }); 
    describe('sh.to_title_case', function() { 
     var output = sh.to_title_case('test_string'); 
     it('should output a title formatted string', function() { 
      expect(output).to.equal('Test String'); 
     }); 
    }); 
    describe('sh.to_snake_case', function() { 
     var output = sh.to_snake_case('Test String'); 
     it('should ouput a snake cased string', function() { 
      expect(output).to.equal('test_string'); 
     }); 
    }); 
    describe('copy_object', function() { 
     var original = { 
      test1: { 
       test2: 'test3' 
      } 
     }; 
     var copy = sh.copy_object(original); 
     it('should create a copy of the original object', function() { 
      expect(copy.test1.test2).to.equal(original.test1.test2); 
     }); 
    }); 

}); 

这里是JavaScript,这是测试:

/** 
* Name: Util.js 
* Description: Contains javascript used on both frontend and backend 
* Note: jQuery Dependent 
*/ 

/** 
* Global namespace for site functionality 
* @type {Object} 
*/ 
if (!sh) { 
    var Site = function() {}, 
     sh = new Site(); 
} 

$(function() { 
    //apply calendar datepicker (jquery ui) to input 
    $('.datePicker').datepicker(); 
    $('.datestampPicker').datepicker({ 
     dateFormat: "yy-mm-dd 00:00:00" 
    }); 

    //Alert confirm message for .confirm buttons 
    $('.confirm').click(function(e) { 
     var confirmation = confirm('Are you sure you want to do this?'); 
     if (!confirmation) 
      e.preventDefault(); 
    });}); 

sh.environment = (function() { 
    var subdomain = window.location.host.split('.')[0]; 
    if(subdomain === 'www') { 
     return 'production'; 
    } else { 
     return 'development'; 
    } 
})(); 

sh.cookies = {}; 
/** 
* Create a cookie 
* @param {string} name name of cookie 
* @param {string} value value of cookie 
* @param {int} days expiration length 
* @return {null}  no return value 
*/ 
sh.cookies.create = function(name, value, days){ 
    var expires = ''; 
    if (days) { 
     var date = new Date(); 
     date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000)); 
     expires = "; expires=" + date.toGMTString(); 
    } 

    document.cookie = name + "=" + value + expires + "; path=/"; 
}; 

/** 
* Read the value of a cookie 
* @param {string} name name of cookie 
* @return {string|null}  value of cookie 
*/ 
sh.cookies.read = function(name){ 
    var nameEQ = name + "="; 
    var ca = document.cookie.split(';'); 
    for (var i = 0; i < ca.length; i++) { 
     var c = ca[i]; 
     while (c.charAt(0) == ' ') c = c.substring(1, c.length); 
     if (c.indexOf(nameEQ) === 0) { 
      return c.substring(nameEQ.length, c.length); 
     } 
    } 
    return null; 
}; 

/** 
* Erase a cookie 
* @param {string} name name of cookie 
* @return {null}  no return value 
*/ 
sh.cookies.erase = function(name){ 
    sh.cookies.create(name, "", -1); 
}; 

/** 
* Get a formatted time from a JavaScript date time object 
* @param {object} time date time 
* @return {string}  formatted date time (hh:mm pm/am) 
*/ 
sh.format_time = function(time){ 
    var minuteFormat = time.getMinutes() < 10 ? '0' + time.getMinutes() : time.getMinutes(); 
    var timeString = 
     time.getHours() - 12 > 0 ? 
     time.getHours() - 12 + ':' + minuteFormat + ' pm' : time.getHours() + ':' + minuteFormat + ' am'; 

    return timeString; 
}; 

/** 
* Formats a string to capital letters with spaces 
* @param {string} str string to be title-cased (test_string) 
* @return {string}  title case version of str (Test String) 
*/ 
sh.to_title_case = function(str){ 
    return str.replace(/_/g, ' ').replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();}); 
}; 

/** 
* Formats a string to lower case with underscores instead of spaces 
* @param {string} str string to be snake cased (Test String) 
* @return {string}  snake case version of str (test_string) 
*/ 
sh.to_snake_case = function(str){ 
    return str.toLowerCase().replace(/ /g,'_'); 
}; 

/** 
* Produces a copy of a javascript object 
* @param {object} object object to be copied 
* @return {object}  copy 
*/ 
sh.copy_object = function(object) { 
    return JSON.parse(JSON.stringify(object)); 
}; 

//returns the version of ie as integer 
sh.get_ie = (function() { 
    var myNav = navigator.userAgent.toLowerCase(); 
    return (myNav.indexOf('msie') != -1) ? parseInt(myNav.split('msie')[1]) : false; 
}()); 

/** 
* Various data structures 
* 
* @type {Object} 
*/ 
sh.structures = {}; 

/** 
* A general exception 
*/ 
sh.structures.Exception = Error; 
+0

我最初的猜测是你想在同步测试中运行异步函数。你可以发布一个跳过测试的例子吗? –

+0

我添加了代码以及其中一个很少运行的文件的测试。这并不是说特定的测试会被跳过。这是测试过早停止,只是没有完成所有的测试。 – Watty

+0

我想我找到了。当我将测试作为单次运行运行时,它会因警告而中止。我删除了三组测试,现在看起来一致。我删除了两个调用'location.reload()'的函数的测试,并且删除了一个将样式表添加到dom的函数的测试。我必须找到一些方法来测试这些没有警告我猜。 – Watty

回答

0

当我将测试作为单次运行运行时,它会因警告而中止。有些关于一些测试正在进行整页重新加载的警告。因此,我删除了两项功能的测试:

  • 请求桌面版本:添加一个cookie并重新加载页面以获取桌面样式。

  • 要求手机版:“完成,没有错误”删除Cookie,然后再重新装入获得移动风格

除去这些之后,我得到的与消息完成的测试数一致

相关问题