单元测试控制器 Ionic Framework Karma

Unit Testing a Controller Ionic Framework Karma

我一直在用头撞墙试图弄清楚为什么这些测试无法使用 Karma 和 Jasmine 来测试离子控制器。我是 ionic、karma 单元测试、angularjs 以及这里几乎所有其他内容的新手。谁能找出这些测试失败的原因?

这是我的控制器:

    angular.module('starter.controllers')
    .controller('TemplateCtrl', function($scope) {
        $scope.text = 'Hello Template';
    });

这是我的测试:

    describe('Template Controller', function(){
        var scope;
        beforeEach(module('starter.controllers'));
        beforeEach(inject(function($rootScope, $controller) {   
            scope = $rootScope.$new();                          
            $controller('TemplateCtrl', {$scope: scope});       
        }));

        // tests start here

        it('should have scope defined', function() {
           expect(scope).toBeDefined();
        });

        it('should have text set to Hello Template', function(){
             expect(scope.text).toEqual('Hello Template');
        });
    });

测试结果:

PhantomJS 2.1.1 (Linux 0.0.0) 模板控制器应该定义范围失败 预期未定义将被定义。

PhantomJS 2.1.1 (Linux 0.0.0) 模板控制器应将文本设置为 Hello 模板失败 TypeError: undefined is not an object (evaluating 'scope.text') in controller-tests/template.tests.js(第 23 行)

感谢您的宝贵时间。

Phil 发现了问题,我什至没有发布我的 karma 配置文件:

Does Karma include the files that define your starter.controllers module as well as the TemplateCtrl controller?

只需将路径添加到 karma 配置文件即可解决我的问题。 谢谢菲尔!