获取 Karma 中的角度常数

Rea*_*ven 4 angularjs karma-runner karma-jasmine ionic-framework

鉴于应用程序启动:

angular.module("starter", [ "ionic" ])
    .constant("DEBUG", true)
    .run(function() {
        /* ... */
    });
Run Code Online (Sandbox Code Playgroud)

我将如何测试 的值DEBUG

当尝试使用:

describe("app", function() {

    beforeEach(function() {
        module("starter");
    });

    describe("constants", function() {
        describe("DEBUG", inject(function(DEBUG) {
            it("should be a boolean", function() {
                expect(typeof DEBUG).toBe("boolean");
            });
        }));
    });
});
Run Code Online (Sandbox Code Playgroud)

我刚刚得到

TypeError: 'null' is not an object (evaluating 'currentSpec.$modules')
    at workFn (/%%%/www/lib/angular-mocks/angular-mocks.js:2230)
    at /%%%/www/js/app_test.js:14
    at /%%%/www/js/app_test.js:15
    at /%%%/www/js/app_test.js:16
Run Code Online (Sandbox Code Playgroud)

Rea*_*ven 5

确保它在正确的位置被实例化。在这种情况下,beforeEach没有运行 来加载模块,因为DEBUG正在块inject()describe而不是it块中进行编辑。以下工作正常:

describe("app", function() {

    var DEBUG;

    beforeEach(function() {
        module("starter");
    });

    describe("constants", function() {
        describe("DEBUG", function() {
            it("should be a boolean", inject(function(DEBUG) {
                expect(typeof DEBUG).toBe("boolean");
            }));
        });
    });
});
Run Code Online (Sandbox Code Playgroud)