返回承诺的茉莉花测试函数

Sai*_*nan 5 javascript tdd unit-testing promise jasmine

我有以下功能实现

function getRepo(url) {
    var repos = {};

    if (repos.hasOwnProperty(url)) {
        return repos[url];
    }

    return $.get(url)
        .then(repoRetrieved)
        .fail(failureHandler);

    function repoRetrieved(data) {

        return repos[url] = data;
    }

    function failureHandler(err, xhr) {
        throw new Error(xhr.responseText);
    }
}
Run Code Online (Sandbox Code Playgroud)

我写了以下测试:

describe('"getRepo" method', function() {
    var getDeffered;
    var $;

     beforeEach(function() {
         getDeffered = Q.defer();
         $ = jasmine.createSpyObj('$', ['get']);
         $.get.and.returnValue(getDeffered.promise);
     });

     it('should return a promise', function(){
         expect(getRepo('someURL')).toEqual(getDeffered.promise);
     });

});
Run Code Online (Sandbox Code Playgroud)

而这个测试失败了。我想是因为我调用了 then 方法。

如果函数实现是:

function getRepo(url) {
    return $.get(url);
}
Run Code Online (Sandbox Code Playgroud)

这是 jasmine 在使用 Q.defer() 时抛出的消息

Expected { promiseDispatch : Function, valueOf : Function, inspect : Function }
to equal { promiseDispatch : Function, valueOf : Function, inspect : Function }.
Run Code Online (Sandbox Code Playgroud)

如果我使用 jQuery Deferred,这就是消息:

Expected { state : Function, always : Function, then : Function, promise : Function, pipe : Function, done : Function, fail : Function, progress : Function } 
to equal { state : Function, always : Function, then : Function, promise : Function, pipe : Function, done : Function, fail : Function, progress : Function }.
Run Code Online (Sandbox Code Playgroud)

jQuery 延迟测试实现:

describe('"getRepo" method', function() {
    var getDeffered;
    var $;

    beforeEach(function() {
        getDeffered = real$.Deferred();
        $ = jasmine.createSpyObj('$', ['get']);
        $.get.and.returnValue(getDeffered.promise());
    });

    it('should return a promise', function(){
        expect(getRepo('someURL')).toEqual(getDeffered.promise());
    });

});
Run Code Online (Sandbox Code Playgroud)

Mad*_*iha 3

您可以直接测试解析值,而不是测试返回的对象是否为 Promise,这会提供更准确的结果:

describe('"getRepo" method', function() {
    var originalGet;
    beforeAll(function() {
        originalGet = $.get;
    });
    beforeEach(function() {
        $.get = originalGet; // Restore original function
    });

    it('should update the data[] array when resolved', function(done) {
    // This is an asynchronous test, so we need to pass done    ^^^^
        var expectedResult = 'test data';
        $.get = function() { 
            var dfd = $.Deferred();
            dfd.resolve(expectedResult);
            return dfd.promise();
        };
        getRepo('someURL').then(function(repos) {
            var actualResult = repos['someUrl'];
            expect(actualResult).toEqual(expectedResult);
            done(); // This is an asynchronous test. We must mark the test as done.
        });
    });
});
Run Code Online (Sandbox Code Playgroud)

请注意,jQuery 的 Promise 实现非常糟糕。最好使用原生 Promises,或者像 Bluebird 这样的库。