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

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

'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/$injector/unpr?p0=%24scopeProvider%20%3C-%20%24scope%20%3C-%20LoginCtrl

我可以在开发者控制台中看到,在因果驱动的浏览器中,控制器和它的依赖文件都被正确加载。

我看不出有什么问题?

UPDATE

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

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/$injector/unpr?p0=%24scopeProvider%20%3C-%20%24scope


这是因为您需要像这样在注入中添加范围:

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

同样,如果你真正的控制器有注射,你将用于测试,你需要添加它们。例如(这只是一个例子):

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

在这里找到了一个有效的答案: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');
}));

我需要阅读如何嘲笑和注射工程!

链接地址: http://www.djcxy.com/p/77667.html

上一篇: Angular controller not injected in test

下一篇: how to inject factory into another factory in Angular