注入(?)mock,jasmine和angular

时间:2015-07-18 16:16:18

标签: javascript angularjs unit-testing jasmine

我是使用Jasmine对Angular进行单元测试的新手,我几个小时一直在努力使用这段代码,我在这里经历了很多文章和答案,但是,我无法找到溶液

您可以看到服务是模拟。问题是,下面的代码抛出了错误,它找不到 authenticationService 变量。但是,根据我今天收集的信息,它应该注入"它"方法

如果我重写代码并且必要的东西被注入"它"方法比它的作品。但是,它很难看,而不是应该遵循的方式,因为它会导致样板代码。

我做错了什么?

我试图移动注入(函数($ rootScope,$ controller)块来创建模拟但是它没有帮助,后来我才知道有一个正确的顺序在注入时应该记住。所以我把它们放回嵌套的描述块中。

测试的目的是检查在触发 event:auth-loginRequired 时是否调用 authenticationService.authenticate()。我不确定代码的断言部分是否正确。一旦我上面描述的问题得到解决,我就会继续努力。

describe('dilibShell module speccifications', function ()
{
    var $location, $rootScope, scope, AuthenticationService, AuthService, Common, ShellController;

    beforeEach(module('dilibShell'));

    beforeEach(function ()
    {
        AuthenticationService = {
            authenticate: function (user)
            {
                return 'user';
            }
        }

        AuthService = {
            loginConfirmed: function () { }
        }

        Common = {
            activateController: function () { },
            logger: {
                getLogFn: function () { }
            }
        }

        module(function ($provide)
        {
            $provide.value('authenticationService', AuthenticationService);
        });

        module(function ($provide)
        {
            $provide.value('authService', AuthService);
        });

        module(function ($provide)
        {
            $provide.value('common', Common);
        });

    });

    describe('shellController Specification', function ()
    {  beforeEach(function ()
        {
            inject(function ($rootScope, $controller)
            {
                rootScope = $rootScope;
                scope = $rootScope.$new();
                ShellController = $controller('shellController', {
                    $scope: scope
                });
            });
        });

        it('When event:auth-loginRequired is caught then authenticationService.authenticate must be invoked.',
            (function ()
            {

                expect(authenticationService.authenticate()).toBe('user');
                //arrange

                //spyOn(authenticationService, 'authenticate');
                scope.$digest();

                //act
                rootScope.$broadcast('event:auth-loginRequired');

                //assert
                expect(authenticationService.authenticate).toHaveBeenCalledWith();


            }));
    });
});

更新

基于答案here我修改了我的代码,块看起来如下,它工作正常,不计算代码中的另一个错误,但是那个已经结束了范围。

所以,我的问题是,在注入服务模拟的情况下,我不得不在特定的块中调用注入?

it('When event:auth-loginRequired is caught then authenticationService.authenticate must be invoked.',
            (inject(function (authenticationService)
            {
                expect(authenticationService.authenticate()).toBe('user');
                //arrange
                spyOn(authenticationService, 'authenticate');
                //scope.$digest();
                //act
                rootScope.$broadcast('event:auth-loginRequired');
                //assert
                expect(authenticationService.authenticate).toHaveBeenCalledWith();


            })));

0 个答案:

没有答案