码迷,mamicode.com
首页 > 编程语言 > 详细

JavaScript 自定义单元测试

时间:2015-04-15 08:22:26      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:

<!doctype html>
<html>
    <head>
        <meta charset="utf-8">
        <script>
        ‘use strict‘;
        var yyTester = (function(){
            var testFunctions = [],
                testDesc = [];

            function describe (desc, func) {
                if (testFunctions.indexOf(func) < 0){
                    testFunctions.push(func);
                    testDesc.push(desc);
                }
            }

            function undescribe (func) {
                var i = testFunctions.indexOf(func);
                if (i >= 0){
                    testFunctions.splice(i, 1);
                    testDesc.splice(i, 1);
                }
            }

            function runTest(){
                for (var i = 0, l = testFunctions.length; i < l; ++i){
                    console.log(testDesc[i]);
                    testFunctions[i]();
                }
            }

            return {
                $describe : describe , 
                $undescribe: undescribe,
                $runTest: runTest
            };
        })();
            
        (function(){
            yyTester.$describe(‘数组测试‘, function(){
                var a = [];
                var aValue = 1;
                a.push(aValue);
                console.assert(a.length == 1);
                console.assert(aValue == a[a.length - 1]);
                console.assert(a.indexOf(aValue) == a.length - 1);
                a.pop();
                console.assert(a.length == 0);
            });
            
            yyTester.$runTest();
        })();
        </script>
    </head>
    <body>
    </body>
</html>

 

JavaScript 自定义单元测试

标签:

原文地址:http://www.cnblogs.com/yaoyu126/p/4427539.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!