QUnit的使用

栾耀
2023-12-01

最近我准备构建一个自己的轻量级的库,但是我遇到了一些问题,最大的问题就是比如写着写着,我怕后面写的东西会覆盖掉前面的一些功能,所以我只能不停的测试,于是我想到了用一些测试库,试着安装传说中的mocha啥的,on my god,我的破电脑完全不给力装不上去,最后找了这一款超级轻量级的语法简单的测试库,而且还是牛逼的John Resig发明的.

 

```

QUnit.test( "isFunction", function( assert ) 
assert.expect( 2 );//指明回调里的断言的次数

function calc( x, operation ) {
return operation( x );
}

var result = calc( 2, function( x ) {
assert.ok( true, "calc() calls operation function" );
return x * x;
});

assert.equal( result, 4, "2 square equals 4" );
});

```

 

 

统计事件执行次数

```

QUnit.test( "a test", function( assert ) {
assert.expect( 1 );

var $body = $( "body" );

$body.on( "click", function() {
assert.ok( true, "body was clicked!" );
});

$body.trigger( "click" );
});

 

```

 

 

分组

 

```

QUnit.module( "group a" );
QUnit.test( "a basic test example", function( assert ) {
assert.ok( true, "this test is fine" );
});
QUnit.test( "a basic test example 2", function( assert ) {
assert.ok( true, "this test is fine" );
});

QUnit.module( "group b" );
QUnit.test( "a basic test example 3", function( assert ) {
assert.ok( true, "this test is fine" );
});
QUnit.test( "a basic test example 4", function( assert ) {
assert.ok( true, "this test is fine" );
});

 

```

 

stub:伪造一个方法

mock:不但伪造,而且这个方法必须执行

转载于:https://my.oschina.net/u/1792175/blog/598063

 类似资料: