• javascript QUnit 单元测试


    <!doctype html>
    <html>
    <head lang="zh-CN" dir="ltr">
        <meta charset="utf-8">
        <title></title>
        <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
    </head>
    <body>
    
    
    <!--qunit start-->
            <h1 id="qunit-header">QUnit Test Suite</h1>  
            <h2 id="qunit-banner"></h2>  
            <div id="qunit-testrunner-toolbar"></div>  
            <h2 id="qunit-userAgent"></h2>  
            <ol id="qunit-tests"></ol> 
    <!--qunit end-->
    
    <link rel="stylesheet" href="http://code.jquery.com/qunit/qunit-1.12.0.css">
    <script type="text/javascript" src="http://code.jquery.com/qunit/qunit-1.12.0.js"></script>
    
    <script type="text/javascript" src="demo.js"></script>
    </body>
    </html>
    test( "ok test", function() {
      ok( true, "true succeeds" );
      ok( "non-empty", "non-empty string succeeds" );
     
      ok( false, "false fails" );
      ok( 0, "0 fails" );
      ok( NaN, "NaN fails" );
      ok( "", "empty string fails" );
      ok( null, "null fails" );
      ok( undefined, "undefined fails" );
    });
    
    test( "strictEqual test", function() {
      strictEqual( 1, 1, "1 and 1 are the same value and type" );
    });
    
    test( "a test", function() {
      notStrictEqual( 1, "1", "String '1' and number 1 don't have the same value" );
    });
    
    
    test( "throws", function() {
     
      function CustomError( message ) {
        this.message = message;
      }
     
      CustomError.prototype.toString = function() {
        return this.message;
      };
     
      throws(
        function() {
          throw "error"
        },
        "throws with just a message, no expected"
      );
     
      throws(
        function() {
          throw new CustomError();
        },
        CustomError,
        "raised error is an instance of CustomError"
      );
     
      throws(
        function() {
          throw new CustomError("some error description");
        },
        /description/,
        "raised error message contains 'description'"
      );
    });
    
    
    asyncTest( "asynchronous test: one second later!", function() {
      expect( 1 );
     
      setTimeout(function() {
        ok( true, "Passed and ready to resume!" );
        start();
      }, 1000);
    });
    
    test( "a test", function() {
      stop();
      asyncOp();
      setTimeout(function() {
        equals( asyncOp.result, "someExpectedValue" );
        start();
      }, 150 );
    });
    
    test( "a test", function() {
      stop();
      asyncOp();
      setTimeout(function() {
        equals( asyncOp.result, "someExpectedValue" );
        start();
      }, 150 );
    });
    
    QUnit.done(function( details ) {
      console.log( "Total: ", details.total, " Failed: ", details.failed, " Passed: ", details.passed, " Runtime: ", details.runtime );
    });
    
    
    asyncTest( "asynchronous test: one second later!", function() {
      expect( 1 );
     
      setTimeout(function() {
        ok( true, "Passed and ready to resume!" );
        start();
      }, 1000);
    });
    
    asyncTest( "asynchronous test: video ready to play", 1, function() {
      var $video = $( "video" );
     
      $video.on( "canplaythrough", function() {
        ok( true, "video has loaded and is ready to play" );
        start();
      });
    });
    
    test( "a test", function() {
      expect( 2 );
     
      function calc( x, operation ) {
        return operation( x );
      }
     
      var result = calc( 2, function( x ) {
        ok( true, "calc() calls operation function" );
        return x * x;
      });
     
      equal( result, 4, "2 square equals 4" );
    });
    
    test( "a test", 2, function() {
     
      function calc( x, operation ) {
        return operation( x );
      }
     
      var result = calc( 2, function( x ) {
        ok( true, "calc() calls operation function" );
        return x * x;
      });
     
      equal( result, 4, "2 square equals 4" );
    });
    
    
    module( "group a" );
    test( "a basic test example", function() {
      ok( true, "this test is fine" );
    });
    test( "a basic test example 2", function() {
      ok( true, "this test is fine" );
    });
     
    module( "group b" );
    test( "a basic test example 3", function() {
      ok( true, "this test is fine" );
    });
    test( "a basic test example 4", function() {
      ok( true, "this test is fine" );
    });
  • 相关阅读:
    php 1011
    php 1008
    php 1012
    php 1007
    解决由于找不到msvcp120.dll无法继续执行代码的问题
    给计算机专业学生的7个建议 来自书圈
    使用sshwifty搭建WebSHH服务器
    Springboot使用Swagger
    SpringBoot 启动时自动执行代码的几种方式
    Docker运行镜像报错处理 docker0: iptables: No chain/target/match by that name.
  • 原文地址:https://www.cnblogs.com/wangwenfei/p/javascript_module.html
Copyright © 2020-2023  润新知