【问题标题】:Getting error in controller while unit testing using karma jasmine使用 karma jasmine 进行单元测试时控制器出错
【发布时间】:2014-11-12 13:32:24
【问题描述】:

我在使用 Karma-Jasmine 运行单元测试时遇到此错误

ReferenceError: myModule 未定义

我的示例测试用例如下..

describe("单元测试", function() {

beforeEach(angular.mock.module('myModule.common'));
var scope, ngTableParams, filter ,testTableParam;

it('should have a commonController controller', function () {
    expect(myModule .common.controller('commonController ', function (commonController ) {
            $scope:scope;
            ngTableParams:ngTableParams;
            $filter: filter;
            tableParams: testTableParam
        }
    )).toBeDefined();
});});

我已将模块名称注入为 myModule.common。 你能提出一个解决方案吗?

【问题讨论】:

  • 代码中myModule.common 之间有空格吗?还是只有您发布的示例?
  • karma.conf.js中声明你的模块的文件吗?
  • 对不起。myModule 和 common 之间没有空格。我已包含声明我的模块的文件
  • 我认为您应该尝试像这样jsfiddle.net/themyth92/dc1bymzm/1 更改您的测试。错误“myModule”未定义发生在“expect(myModule.common.controller(...”)这一行,实际上您的测试中没有“myModule.common”对象。

标签: angularjs unit-testing jasmine karma-jasmine


【解决方案1】:

尝试以下代码 sn-p 可能会有所帮助

describe('testing myModule.common', function() {
    var $rootScope, $scope, $filter, $controller, ngTableParams, testTableParam;
    beforeEach(module('myModule.common'));
    beforeEach(function() {
        inject(function($injector) {
            $rootScope = $injector.get('$rootScope');
            $scope = $rootScope.$new();
            $filter = $injector.get('$filter');
            testTableParam = $injector.get('testTableParam');
            ngTableParams = $injector.get('ngTableParams');
            $controller = $injector.get('$controller')('commonController ', {
                $scope: $scope
            });

        });
    });
    it('testing commonController ', function() {
        expect('commonController ').toBeDefined();
    });
});

它会解决你的问题

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-30
    • 1970-01-01
    • 1970-01-01
    • 2014-08-07
    • 1970-01-01
    • 2018-04-06
    • 1970-01-01
    相关资源
    最近更新 更多