使用promises对Angular服务进行单元测试失败

时间:2015-10-12 13:46:03

标签: angularjs unit-testing jasmine nools

JSFiddle显示问题:

http://jsfiddle.net/ggvfwoeL/3/

我有一个Angular应用程序,涉及一个用于生成威胁的服务。威胁生成使用nools规则引擎完成。该服务公开了一个返回promise的函数generateForElement。该函数如下所示(有关详细信息,请参阅JSFiddle):

var Element = function (element) { this.type = element.attributes.type; }
var Threats = function () { this.collection = []; }    

    function generateForElement(element) {
           var threats = new Threats();
           var el = new Element(element);
           flow.getSession(threats, el).match();
           return $q.when(threats.collection);
    }

设置规则引擎,以便在使用generateForElement类型的元素调用tm.Process时,会生成2个威胁。以下是定义规则的代码(显然,为了使问题更加清晰,这是非常简化的):

function initialiseFlow() {
        return nools.flow('Element threat generation', function (flow) {

            flow.rule('Spoofing Threat Rule', [
                [Element, 'el', 'el.type == "tm.Process"'],
                [Threats, 'threats']
            ], function (facts) {
                facts.threats.collection.push('Spoofing');
            });

            flow.rule('Tampering Threat Rule', [
                [Element, 'el', 'el.type == "tm.Process"'],
                [Threats, 'threats']
            ], function (facts) {
                facts.threats.collection.push('Tampering');
            });
        });
    }

当我在我的应用程序中手动测试时,我看到正在生成2个威胁。但我的单元测试在这一行失败了

expect(threats.length).toEqual(2);

有错误

Error: Expected 1 to equal 2.

因此,似乎只生成了1个威胁。单元测试定义如下:

describe('threatengine service', function () {

    var threatengine;
    var $rootScope;

    beforeEach(function () {

        angular.mock.module('app')

        angular.mock.inject(function (_$rootScope_, _threatengine_) {
            threatengine = _threatengine_;
            $rootScope = _$rootScope_;
        });

        $rootScope.$apply();
    });

    describe('threat generation tests', function () {

        it('process should generate two threats', function () {

            var element = { attributes: { type: 'tm.Process' }};
            var threats;
            threatengine.generateForElement(element).then(function (data) {
                threats = data;
            });
            expect(threats).toBeUndefined();
            $rootScope.$apply();
            expect(threats).toBeDefined();
            expect(threats.length).toEqual(2);
        });
    });
});

显然我做错了什么。正如我所说,当我运行完整的应用程序时,我肯定得到2个威胁,这让我觉得故障是单元测试,或者可能是我如何处理我的服务中的承诺,但我只是看不到它。

为什么我的单元测试失败?

1 个答案:

答案 0 :(得分:2)

第一个问题出现在flow.getSession(threats, el).match();调用中,在您的代码中同步调用,但最初看起来是异步的(我不熟悉nools,但是here are the docs)。因此,即使您将console.log置于两个规则的处理程序中,您也会发现规则的处理方式比后续同步代码更晚。它的解决方案是使用一个.match()返回的承诺:

function generateForElement(element) {
    var threats = new Threats();
    var el = new Element(element);
    // handle async code via promises and resolve it with custom collection
    return flow.getSession(threats, el).match().then(function () {
      return threats.collection;
    });
}

另一个问题是在测试文件中。在那里,您还有异步代码,但您可以像处理同步代码一样处理它。见Asynchronous Support in Jasmine docs。基本上,如果你的测试是异步的,你必须告诉Jasmine,并在完成后通知它。

it('process should generate two threats', function (done) {
    // telling Jasmine that code is async ----------^^^

    var element = { attributes: { type: 'tm.Process' }};

    // this call is async
    threatengine.generateForElement(element).then(function (threats) {
        expect(threats).toBeUndefined();
        expect(threats).toBeDefined();
        expect(threats.length).toEqual(2);

        done(); // telling Jasmine that code has completed
    });

    // is required to start promises cycle if any
    $rootScope.$apply();
});

Here is a working JSFiddle

<强>更新

以下是Jasmine 1.3的规范,它使用another API进行异步流程:

it('process should generate two threats', function (done) {

    var element = { attributes: { type: 'tm.Process' }};
    var threats;

    runs(function () {
        threatengine.generateForElement(element).then(function (data) {
            threats = data;
        });
        $rootScope.$apply();
        });

        waitsFor(function () {
            return typeof threats !== 'undefined';
    });

    runs(function () {
        expect(threats).not.toBeUndefined();
        expect(threats).toBeDefined();
        expect(threats.length).toEqual(20);
    });

});