我对AngularJS
很新,甚至更新于Jasmine
的单元测试。我必须写一个测试:
$scope.$watch(
function () {
return alertService.getAlert();
},
function (newVal, oldVal)
{
if (typeof newVal !== 'undefined') {
$scope.alert = alertService.getAlert();
}
});
getAlert
返回的内容如下:
[ Object({ type: 'typeOfAlert', message: 'alertMessage' }) ].
现在,我已经了解了(在某些情况下甚至可以理解)关于消化,超时,模拟服务等的事情,但我只知道如何处理变速手表,我不知道该怎么做:
alertService.getAlert();
有人能帮我一把吗?
好的,两天后我发现了自己的错误。只需要注入服务
beforeEach(inject(function ($rootScope, $controller, _$timeout_, _alertsService_) {
scope = $rootScope.$new();
controller = $controller('mainPageController', {
'$scope': scope
});
alertService = _alertService_;
}));
和测试整个事情
it('should fire getAlerts when alert is appended', function(){
alertService.appendAlert('success', 'TESTMESSAGE' );
alertService.getAlert();
scope.$digest();
expect(scope.alerts).toEqual([
Object({ type: 'success', message: 'TESTMESSAGE' })
]);
})
现在感觉很尴尬,但我希望将来有人有同样的问题。