【发布时间】: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