角度控制器未在测试中注入

时间:2016-11-22 16:50:40

标签: angularjs karma-jasmine

我有一个简单的登录控制器:

'use strict';

angular.module('login', ['ngRoute'])
    .config(['$routeProvider', function ($routeProvider) {

    }])
    .controller('LoginCtrl', ["$scope", "$route", "LoginService", function ($scope, $route, LoginService) {
        var self = this;
        this.showGuestLogin = true;
        this.showUserLogin = false;

        this.toggleUserLoginType = function () {
            this.showGuestLogin = !this.showGuestLogin; 
            this.showUserLogin = !this.showUserLogin;
        }

        this.submitGuestLogin = function()
        {
            if(this.guestName === undefined || this.guestName.trim() == '')
            {
                self.loginError = "Name cannot be blank";
                return; 
            }

            LoginService.loginAsGuest(this.guestName.trim())
            .then(function()
            {
                self.loginError = null;
                $route.reload();
            })
            .catch(function(err)
            {
                self.loginError = 'An error occured. Please try again';
            });
        }
    }]);

我试图用以下方法测试它:

describe('LoginCtrl', function()
{
    beforeEach(module('login'));

    var ctrl;

    beforeEach(inject(function($controller)
    {
        ctrl = $controller('LoginCtrl');
    }));

    it('should set error if guest name is undefined', function(done)
    {
        ctrl.guestName = undefined;
        ctrl.submitGuestLogin();
        expect(ctrl.loginError).toBeDefined();
    });
});

但是我在测试运行时在控制台中收到此错误

  

错误:[$ injector:unpr]   http://errors.angularjs.org/1.5.8/ $注射器/ unpr?P0 =%24scopeProvider%20%3 C-%20%24scope%20%3 C-%20LoginCtrl

我可以在karma驱动的浏览器的开发者控制台中看到控制器及其相关文件都正确加载。

我看不出有什么问题?

更新

我尝试过传递空对象的建议:

beforeEach(inject(function($controller, $scope, $route, LoginService)
{
    ctrl = $controller('LoginCtrl', { 

    });
}));

并设置依赖项:

beforeEach(inject(function($controller, $scope, $route, LoginService)
{
    ctrl = $controller('LoginCtrl', { 
        $scope: $scope,
        $route: $route,
        LoginService: LoginService
    });
}));

这两个都给我这个错误:

  

错误:[$ injector:unpr]   http://errors.angularjs.org/1.5.8/ $注射器/ unpr?P0 =%24scopeProvider%20%3 C-%20%24scope

2 个答案:

答案 0 :(得分:2)

这是因为您需要在注入中添加范围,如下所示:

beforeEach(inject(function($controller, $scope) {
    ctrl = $controller('LoginCtrl', { $scope: $scope });
}));

同样,如果您的真实控制器具有您将用于测试的注射,则需要将它们添加进来。例如(这只是一个示例):

ctrl = $controller('LoginCtrl',
  {
    $scope: $scope,
    SomeService: SomeService,
    moment: moment,
    dateFormat: dateFormat
  });

答案 1 :(得分:0)

在这里找到了一个有效的答案:Angular Unit Test Unknown provider: $scopeProvider

beforeEach(inject(function($controller, $rootScope, $route, LoginService)
{
    scope = $rootScope.$new();

    ctrl = $controller('LoginCtrl', { 
        $scope: scope
    });
}));

在我的情况下,我实际上并不需要将$ scope注入我的控制器,所以我删除了原来的代码现在可以工作:

beforeEach(inject(function($controller, $rootScope, $route, LoginService)
{
        ctrl = $controller('LoginCtrl');
}));

我需要了解模拟和注射的工作原理!