简体   繁体   English

如何在AngularJS中使用Jasmine测试$ interval

[英]How to test $interval with Jasmine in AngularJS

I have a service Home and inside it, I have selectInterval function like so: 我有一个家庭服务,它里面有selectInterval函数,如下所示:

Home.selectInterval = function () {
    var interval = $interval(function () {
        if (angular.element('.ui-select-toggle').length > 0) {
            angular.element('.ui-select-toggle').bind('click', function () {
                if (angular.element('.ui-select-choices-row').length > 0) {
                    angular.element('.ui-select-choices-group').mCustomScrollbar();
                }
            });
            $interval.cancel(interval);
        }
    }, 50);
};

Right now I am testing it like this: 现在,我正在像这样测试它:

  it('selectInterval() should be called.', function () {
    Home.selectInterval();
    $interval.flush(50);
    $rootScope.$apply();
    expect(Home.selectInterval).toHaveBeenCalled();
    // need to test $interval as well
});

I want to test if function is called and also $interval worked fine. 我想测试函数是否被调用,并且$ interval也可以正常工作。 Right now It giving me this error. 现在,它给了我这个错误。

Some of your tests did a full page reload!

I'm not sure if this test is what is causing your issue. 我不确定此测试是否是引起您问题的原因。 Whenever I have seen that error it is because there is some code that is changing the url. 每当我看到该错误时,都是因为有一些代码正在更改url。 Setting something like $window.location.href or $location.path('/new-path') will cause the phantomjs browser to do a page reload which there just isn't any support to handle right now. 设置类似$window.location.href$location.path('/new-path')将导致phantomjs浏览器重新加载页面,目前尚无任何支持。

If you find this is the issue, you just need to try to spy the method and it will never actually call it. 如果发现这是问题所在,则只需尝试监视该方法,它将永远不会真正调用它。 That specifically works for $location.path('/new-path') 这特别适用于$location.path('/new-path')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM