Jasmine 中的单元测试 angularjs $watch
Unit testing angularjs $watch in Jasmine
我是 AngularJS
的新手,甚至是 Jasmine
中的单元测试新手。我必须为以下内容编写测试:
$scope.$watch(
function () {
return alertService.getAlert();
},
function (newVal, oldVal)
{
if (typeof newVal !== 'undefined') {
$scope.alert = alertService.getAlert();
}
});
getAlert
returns 类似:
[ 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' })
]);
})
现在感觉很尴尬,希望以后有人遇到同样的问题。
我是 AngularJS
的新手,甚至是 Jasmine
中的单元测试新手。我必须为以下内容编写测试:
$scope.$watch(
function () {
return alertService.getAlert();
},
function (newVal, oldVal)
{
if (typeof newVal !== 'undefined') {
$scope.alert = alertService.getAlert();
}
});
getAlert
returns 类似:
[ 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' })
]);
})
现在感觉很尴尬,希望以后有人遇到同样的问题。