在Jasmine单元测试angularjs $ watch

问题描述 投票:1回答:1

我对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();

有人能帮我一把吗?

angularjs jasmine karma-jasmine watch
1个回答
1
投票

好的,两天后我发现了自己的错误。只需要注入服务

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' })
    ]);
})

现在感觉很尴尬,但我希望将来有人有同样的问题。

© www.soinside.com 2019 - 2024. All rights reserved.