Angular测试无法使用Jasmine和Karma实例化模块

时间:2017-12-06 17:56:40

标签: angularjs unit-testing jasmine karma-jasmine ngmock

我已经使用Angular 1.6.6,ng-mock 1.6.6,Jasmine和Karma建立了测试环境。但即使是最简单的测试,我在尝试注入deloreanApp模块时也会获得[$ injector:modulerr]

  

由于以下原因无法实例化模块deloreanApp:错误:   [$注射器:NOMOD]

理论上没有tipo错误,Angular和ng-mock版本匹配。

我的档案是:

app.js

SELECT
    t1.x,
    t1.y,
    COUNT(*) as [count]
FROM
    Table1 t1
    INNER JOIN Table2 t2 ON t1.y = t2.y
WHERE
    t1.y IS NOT NULL
GROUP BY
    t1.x, t1.y
UNION ALL
SELECT
    t1.x,
    NULL as y,
    t2missingFromt1.[count]
FROM
    Table1 t1
    CROSS APPLY (
        SELECT
             COUNT(x) as [count]
        FROM
             Table2 t2
        WHERE NOT EXISTS (SELECT 1 FROM Table1 tbl1 WHERE tbl1.y = t2.y)
    ) t2missingFromt1
WHERE
    t1.y IS NULL

controllers.js

(function () {
    "use strict";

    // initialize Angular
    angular.module('deloreanApp', ['deloreanApp.controllers', 'deloreanApp.services']);

    angular.module('deloreanApp.controllers', []);
    angular.module('deloreanApp.services', []);
})();

DeloreanController.test.js

(function () {
    "use strict";


function deloreanController($scope){
    $scope.sum = function(a,b){
        return a + b;
    }
}

  angular.module('deloreanApp.controllers', []).controller('DeloreanController', ['$scope', deloreanController] );
})();

我的 karma.conf.js 文件的一部分:

describe('calculator', function () {

    beforeEach(module('deloreanApp'));

    var $controller;

    beforeEach(inject(function (_$controller_) {
        $controller = _$controller_;
    }));

    describe('sum', function () {
        it('1 + 2 should equal 3', function () {
            var result = 3;
            expect(result).toBe(3);
        });
    });

});

1 个答案:

答案 0 :(得分:0)

试试这个,

(function () {
    "use strict";
    angular.module('deloreanApp.controllers', []);
    angular.module('deloreanApp.services', []);
    // initialize Angular
    angular.module('deloreanApp', ['deloreanApp.controllers', 'deloreanApp.services']);
})();