5

我使用 Jack 作为 JavaScript 模拟库。http://github.com/keronsen/jack。我也在使用 qunit。

我在我的 javascript 代码中有以下 AJAX 调用,我正在尝试为其编写测试。

$.ajax({
    url: $('#advance_search_form').attr('action'),
    type: 'post',
    dataType: 'json',
    data: parameterizedData,
    success: function(json) {
        APP.actOnResult.successCallback(json);
    }
});

以下代码正在工作。

jack(function() {
    jack.expect('$.ajax').exactly('1 time');
}

但是我想测试是否所有参数都正确提交。我尝试了以下但没有奏效。

jack.expect('$.ajax').exactly('1 time').whereArgument(0).is(function(){

var args = 参数;ok(' http://localhost:3000/users ', args.url, 'url 应该是有效的'); // 对象的许多键的相似测试 });

我想掌握论据,以便进行一系列测试。

4

1 回答 1

4

Two approaches:

Use .hasProperties():

jack.expect('$.ajax').once()
    .whereArgument(0).hasProperties({
         'type': 'post',
         'url': 'http://localhost:3000/users'
    });

... or capture the arguments and make qunit assertions:

var ajaxArgs;
jack.expect('$.ajax').once().mock(function() { ajaxArgs = arguments[0]; });
// ... the code that triggers .ajax()
equals('http://localhost:3000/users', ajaxArgs.url);

The first version uses more of the Jack API (that deserves better documentation), and is more readable, IMO.

The latter version will give you much better error reporting.

于 2010-02-16T19:42:57.580 回答