【发布时间】:2016-07-13 16:16:46
【问题描述】:
我正在尝试编写一个简单的单元测试。我只需要测试我的函数是否被调用。在我的服务中,我有一个简单的方法可以调用另一个方法,像这样
svc.getNewestNotifications = function getNewestNotifications() {
getNewNotifications(username);
};
在我的测试中:
describe('notification service tests', function () {
var $rootScope, $http, $q, notificationSvc, $httpBackend;
beforeEach(module('myApp'));
beforeEach(inject(function(_$rootScope_,_$http_,_$httpBackend_,_$q_,_$sce_,_notificationsFeedService_){
$rootScope = _$rootScope_;
$httpBackend = _$httpBackend_;
$http = _$http_;
$q = _$q_;
notificationSvc = _notificationsFeedService_;
_scope_ = $rootScope.$new();
$scope = _scope_;
$httpBackend.whenGET(/\.html$/).respond('');
}));
describe("getNewestNotifications test", function() {
it('calls the getNewestNotifications when scroll to top', function() {
spyOn(notificationSvc, 'getNewestNotifications').and.callThrough();
expect(notificationSvc.getNewestNotifications).toHaveBeenCalled();
});
});
}
它的“describe(”getNewestNotifications test”, function() {}”块是我的问题。我在控制台中收到“Expected spy getNewestNotifications to have been called.”。我对单元测试很陌生我完全不知道为什么我会看到这个我只是想测试该方法确实被调用了。有什么帮助吗?
【问题讨论】:
-
我
svc.getNewestNotifications这个方法被调用了 -
@PankajParkar 我不明白你的评论
-
在
it部分你应该调用一些东西,现在你只有期望。为什么应该调用notificationSvc.getNewestNotifications- 由什么调用? -
@KrzysztofSafjanowski 这是一个非常好的问题。我想这就是我如此困惑的原因。这对我来说没有任何意义。我不知道这怎么可能起作用。 Javascript 是有道理的,但是我完全迷失了这个单元测试的东西。我想我认为测试会知道服务应该调用该方法。我不知道。无论如何谢谢。
标签: javascript angularjs unit-testing karma-jasmine