本文介绍了$ q和因果报应,摩卡&的角度承诺柴的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我试图在我的有角度的应用程序测试中兑现承诺,任何人都可以找出我在哪里做错了,但它会不断返回:

So i tried to get the promises to work in my angular app tests, can anyone figure out what im doing wrong here it keeps returning:

Error: timeout of 2000ms exceeded. Ensure the done() callback is being called in this test.

我不知道这是不是$ q.

I don't know if it's $q.

仅供参考,我还尝试了it('test', function(done){... done();})

控制器

(function() {
    'use strict';

    angular
        .module('controller.editor', [])
        .controller('EditorController', EditorController);

    function EditorController($scope, $q) {
        var vm = this;

        angular.extend(vm, {
            hack: hack
        });

        function hack(bool) {
            return $q(function(resolve, reject) {
                if (bool) {
                    resolve(true);
                }

                reject(false);
            });
        }
    }
});

测试

describe('EditorController', function() {
    var vm, scope, $controller, $rootScope, $injector;

    beforeEach(function() {
        module('app');

        //Inject
        inject(function(_$injector_) {
            $injector = _$injector_;
            $controller = $injector.get('$controller');
            $rootScope = $injector.get('$rootScope');

            // Create new scope object
            scope = $rootScope.$new();

            // Bind the controller
            vm = $controller('EditorController', {
                $scope: scope
            });
        });
    });

    describe('#addCustom', function() {
        it('test', function(done) {
            var aHack = vm.hack(true);

            aHack.then(function(bool){
                // Resolve
                expect(bool).to.be.eq(true);
                done();
            }, function() {
                // Reject
                expect(bool).to.be.eq(false);
                done();
            });
        });
    });
});

推荐答案

在用角度测试诺言时,最好的方法是依靠角度机器来同步而不是异步地解决诺言.

When testing promises in angular, it's a best practice to depend on angular machinery to do its job to resolve promises synchronously instead of asynchronously.

这使代码更易于阅读和维护.

This makes the code easier to read and maintain.

它也不太容易出错;在.then()中执行断言是一种反模式,因为如果从不调用回调,则断言将永远不会运行.

It's also less error prone; doing assertions in .then() is an anti-pattern because if the callback is never called, your assertions will never run.

要使用角度测试方式,您应该:

To use the angular way of testing, you should:

  1. 删除done
  2. 在测试中
  3. 执行$rootScope.$digest()来解决承诺
  4. 做你的断言
  1. remove done
  2. do $rootScope.$digest() in the test to resolve promises
  3. do your assertions

将其应用于您的代码将是:

Applying this to your code would be:

describe('#addCustom', function() {
    it('test', function() {
        var __bool = false;
        var aHack = vm.hack(true).then(function(bool) {
            __bool = bool;
        });

        $rootScope.$digest();

        expect(__bool).to.be.eq(true);
    });
});

但是这很棘手,因为$rootScope.$digest仅解析$q承诺,而不是所有承诺,特别是不能解析通过Promise()构造函数从各种es5垫片创建的承诺,请参见:

But it's tricky, because $rootScope.$digest resolves only $q promises, not all promises, particularly not the promises created via Promise() constructor from various es5 shims, see this:

在angularjs茉莉花测试中解决的承诺太晚了

另请参阅:

http://brianmcd.com/2014/03/27/a-tip-for-angular-unit-tests-with-promises.html

这篇关于$ q和因果报应,摩卡&的角度承诺柴的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 21:54