【问题标题】:Angularjs: mock location.path() with spyOn for a unit testAngularjs:使用 spyOn 模拟 location.path() 进行单元测试
【发布时间】:2015-05-01 19:50:57
【问题描述】:

我已经阅读了这个post(和其他人),但我无法让这个简单的单元测试工作。我正在使用 Jasmine 的第 2 版。 我的工厂很简单:

angular.module('myApp')
    .factory('detectPath', function ($location, $rootScope) {
        'use strict';
        var locationPath = $location.path()
        function getPath () {
            if (locationPath === '/') {
                locationPath = 'home';
            } else {
                locationPath = '';
            }
            $rootScope.path = locationPath;
        }
        getPath();
        return locationPath;
    });

我的单元测试也很简单:

'use strict';
describe('Factory: detectPath', function () {
    var detectPath, $rootScope, $location;

    beforeEach(module('myApp'));
    beforeEach(inject(function (_detectPath_, _$rootScope_, _$location_) {
        detectPath = _detectPath_;
        $rootScope = _$rootScope_;
        $location = _$location_;
        spyOn($location, 'path').and.returnValue('/');
    }));

    it('should return pathName', function ($location) {
        expect($rootScope.path).toBe('home');
    });
});

这没有通过测试(我得到错误期望 false 是“家”)。

我做错了什么? 有没有办法验证 spyOn 是否已被调用(仅一次)?

【问题讨论】:

    标签: angularjs unit-testing jasmine spyon


    【解决方案1】:

    您的代码有两个主要问题。

    首先,您的getPath() 函数在您设置间谍之前执行。您应该在之前的beforeEach 中设置间谍或在测试中注入您的工厂(我选择了第二种解决方案)。

    第二个问题(尚未影响测试)是您使用测试的函数参数隐藏了 $location 变量 - 您将无法访问它,因为它始终未定义。删除此参数后,我可以测试是否已使用 expect(...).toHaveBeenCalled() 调用了 spy。

    这是一个工作代码:

    describe('Factory: detectPath', function () {
        var detectPath, $rootScope, $location;
    
        beforeEach(module('myApp'));
        beforeEach(inject(function (_$rootScope_, _$location_) {
            $rootScope = _$rootScope_;
            $location = _$location_;
            spyOn($location, 'path').and.returnValue('/');
        }));
    
        it('should return pathName', function () {
            inject(function (detectPath) {
                expect($location.path).toHaveBeenCalled();
                expect($rootScope.path).toBe('home');
            });
        });
    });
    

    还有JSFiddle(使用 Jasmine 1.3,但此示例中的唯一区别是您在 Jasmine 2 中调用 and.returnValue,在 Jasmine 1.3 中调用 returnValue)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-16
      • 1970-01-01
      • 1970-01-01
      • 2016-03-29
      • 2023-03-29
      相关资源
      最近更新 更多