单元测试承诺$ q通话

时间:2020-01-30 14:05:07

标签: angularjs jasmine

$scope.load = function() {
    deferred = $q.defer();
    myService.send('SomeRequestMessage');

    deferred.promise.then(function(data) {
        var modalPopup = loadModal.open('TITLE', data);

        modalPoupup.result.then(function(action) {
            if(angular.isDefined(action.dirName)) {
               myService.sendReq(action.dirName);
            }
        });
    });
};

我对上述功能进行了单元测试,

describe('my tests, ', function () {
    var deferred;
    var lRootScope;
    var lmodalInstance;

    beforeEach(inject(function($q, $rootScope) {
        deferred = $q.defer();
        lRootScope = $rootScope;
        lmodalInstance = sinon.stub( { open: function() {}, close: function() {} } );
        lmodalInstance.open.returns({ result: deferred.promise});
    }));

    xit('should call the popup open method once when the open button is pressed.', function () {

        var callBack =sinon.stub();           
        scope.load();           
        expect(lModalInstance.open.calledOnce).toBe(true);
    });

此测试失败,并抛出"Expected false to be true."

可能在代码行下方执行,并且单元测试期望方法会立即验证而不验证promise(不确定)。

myService.send('SomeRequestMessage');

请帮助修复此测试。

0 个答案:

没有答案