【发布时间】:2014-08-26 15:01:33
【问题描述】:
开始使用HotTowelAngular 模板,我正在设置单元测试。陷入困境。
我正在尝试在我的控制器中测试一个函数,该函数恰好调用了另一个名为“log”的函数。这个“日志”是一个存储在私有变量中的函数,它从一个名为“common”的依赖项中获取其值。
我知道我可能需要以某种方式存根这个函数,但我不确定从哪里开始这个特定的例子,因为我对 angularjs、jasmine 等都很陌生。任何想法表示赞赏
单元测试:
describe("quote", function () {
var scope,
controller,
common;
beforeEach(inject(function($rootScope, $controller, _common_) {
scope = $rootScope.$new();
common = _common_;
controller = $controller;
}));
describe("removeAttachment", function() {
it("should remove the attachment when requested", function() {
var vm = controller("quote", { $scope: scope });
vm.attachmentList.push({ FileName: "file1", FileAsString: "..." });
vm.attachmentList.push({ FileName: "file2", FileAsString: "..." });
vm.attachmentList.push({ FileName: "file3", FileAsString: "..." });
expect(vm.attachmentList.length).toEqual(3);
// here's the call where it fails
vm.removeAttachment(vm.attachmentList[1]);
expect(vm.attachmentListCount).toEqual(2);
expect(vm.attachmentList.length).toEqual(2);
expect(vm.attachmentList[1].FileName).toBe("file3");
});
});
});
控制者:
var getLogFn = common.logger.getLogFn;
var log = getLogFn(controllerId);
function removeAttachment(attachment) {
// need to stub this out
log('removing attachment: ' + attachment.FileName);
vm.attachmentList.splice(vm.attachmentList.indexOf(attachment), 1);
vm.attachmentListCount = vm.attachmentList.length;
}
Jasmine 的错误:
TypeError: 'undefined' is not a function(评估 'log('removing attachment: ' + attachment.FileName)')
【问题讨论】:
标签: javascript angularjs unit-testing jasmine hottowel