【发布时间】:2015-10-07 19:51:31
【问题描述】:
我有一个像这样实现的简单服务
sameRoof
.factory('dbService', function (localStorageService, backendUpdate) {
return {
checkProfileAndFlat: function () {
return (localStorageService.get('profile') && localStorageService.get('flatshare'));
}
};
});
LocalStorage 是使用 bower 安装的 ngModules。
我正在编写单元测试
'use strict';
describe('Service: service taking care of asking the local database', function () {
var localStorageService;
var fakeDB = {'profile' : 'testProfile', 'flatshare' : 'flatshare'};
// load the service's module
beforeEach(module('frontApp'));
// instantiate service
var dbService;
beforeEach(inject(function (_dbService_, _localStorageService_) {
dbService = _dbService_;
localStorageService = _localStorageService_;
//mock localStorageService get/add
spyOn(localStorageService,'get').andCallFake(function(key){
return fakeDB[key];
});
}));
it('should check profile and flatshare', function () {
console.log(localStorageService.get('profile'));
expect( dbService.checkProfileAndFlat() ).toBe(false);
});
});
但是我这里有问题,
TypeError: 'undefined' is not a function (evalating 'spyOn ...)
似乎我以错误的方式实现了 spyOn
【问题讨论】:
-
什么版本的茉莉花?
-
我将假设您使用 2.0 + 并在此处推荐您:stackoverflow.com/a/22043364/2948906
-
@CainBot 即使使用 .and.fakeCall 也无法正常工作
-
假设你的意思是“and.callFake”???
-
非常感谢。它是小写 C 的 callFake
标签: javascript angularjs mocking