[英]Unit testing angularjs $watch in Jasmine
I'm pretty new to AngularJS
and even newer to unit tests in Jasmine
.我对
AngularJS
很AngularJS
,甚至对Jasmine
单元测试也很AngularJS
。 I have to write a test for:我必须为以下内容编写测试:
$scope.$watch(
function () {
return alertService.getAlert();
},
function (newVal, oldVal)
{
if (typeof newVal !== 'undefined') {
$scope.alert = alertService.getAlert();
}
});
getAlert
returns things like: getAlert
返回如下内容:
[ Object({ type: 'typeOfAlert', message: 'alertMessage' }) ].
Now, I've learned (in some cases even understood) things about digest, timeouts, mocking services etc. but I only know how to handle variable-fired watches and I'm not sure what to do with:现在,我已经了解(在某些情况下甚至了解)有关摘要、超时、模拟服务等的知识,但我只知道如何处理可变触发的手表,我不知道该怎么做:
alertService.getAlert();
Could anyone give me a hand?有人可以帮我吗?
Okay, after two days I found my mistake.好的,两天后我发现了我的错误。 Just needed to inject service in
只需要注入服务
beforeEach(inject(function ($rootScope, $controller, _$timeout_, _alertsService_) {
scope = $rootScope.$new();
controller = $controller('mainPageController', {
'$scope': scope
});
alertService = _alertService_;
}));
and test whole thing with并测试整个事情
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' })
]);
})
It feels embarassing now, but I hope someone has same problem in the future.现在感觉很尴尬,但我希望将来有人有同样的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.