angularjs – 获取Karma中的角度常数

鉴于应用启动:

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

我该如何测试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");
            });
        }));
    });
});

我得到了

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

解决方法

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

describe("app",function() {

    var DEBUG;

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

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

相关文章

ANGULAR.JS:NG-SELECTANDNG-OPTIONSPS:其实看英文文档比看中...
AngularJS中使用Chart.js制折线图与饼图实例  Chart.js 是...
IE浏览器兼容性后续前言 继续尝试解决IE浏览器兼容性问题,...
Angular实现下拉菜单多选写这篇文章时,引用文章地址如下:h...
在AngularJS应用中集成科大讯飞语音输入功能前言 根据项目...
Angular数据更新不及时问题探讨前言 在修复控制角标正确变...