Angularjs测试(茉莉花) - $ HTTP返回“不挂起请求刷新” [英] Angularjs testing (Jasmine) - $http returning 'No pending request to flush'

查看:175
本文介绍了Angularjs测试(茉莉花) - $ HTTP返回“不挂起请求刷新”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的UserManager服务会自动触发一个$ HTTP POST每隔一小时刷新用户访问令牌。

我试图嘲弄这一呼吁,以验证令牌被刷新,但是当我尝试刷新$ httpbackend我得到一个错误说没有挂起的请求,刷新即使我知道,刷新函数被称为(添加的console.log只是为了验证)。

无论是事实函数被调用通过的setTimeout被别人影响的$ httpbackend还是我失去了一些东西。

code附波纹管:

 描述(的UserManager服务测试功能(){    beforeEach(angular.mock.module('Web应用程序'));    beforeEach(注(函数($ httpBackend){
        window.apiUrls = jQuery.parseJSON('{cover_art:http://myrockpack.com/ws/cover_art/?locale=en-us,channel_search_terms:http://myrockpack.com/ws/ ?完整/渠道/区域设置= EN-US,注册:http://myrockpack.com/ws/register/,类别:http://myrockpack.com/ws/categories/?locale = EN-US,reset_password:http://myrockpack.com/ws/reset-password/,SHARE_URL:http://myrockpack.com/ws/share/link/,video_search :http://myrockpack.com/ws/search/videos/?locale=en-us,channel_search:http://myrockpack.com/ws/search/channels/?locale=en-us ,video_search_terms:http://myrockpack.com/ws/complete/videos/?locale=en-us,popular_channels:http://myrockpack.com/ws/channels/?locale=en -us,popular_videos:http://myrockpack.com/ws/videos/?locale=en-us,登陆:http://myrockpack.com/ws/login/,login_register_external :http://myrockpack.com/ws/login/external/,refresh_token:http://myrockpack.com/ws/token/}');
        VAR mockLogin = {\"token_type\":\"Bearer\",\"user_id\":\"oCRwcy5MRIiWmsJjvbFbHA\",\"access_token\":\"752a4f939662846a787a1474ad17ffddcd816dc7AAFB1G7HvgH-0qAkcHMuTESIlprCY72xWxyiuhySCpFJxKVYqOx9W7Gt\",\"resource_url\":\"http://myrockpack.com/ws/oCRwcy5MRIiWmsJjvbFbHA/\",\"expires_in\":2,\"refresh_token\":\"fa2f47f3590240e4bdfdbde03bf8042d\"}
        VAR refreshToken = {\"token_type\":\"Bearer\",\"user_id\":\"CeGfSz6dQW2ga2P2tKb3Bg\",\"access_token\":\"ef235de46dba53ba69ed049f57496ec902da5d28AAFB1HdeTE-2vwnhn0s-nUFtoGtj9rSm9waiuhySCpFJxKVYqOx9W7Gt\",\"resource_url\":\"http://myrockpack.com/ws/CeGfSz6dQW2ga2P2tKb3Bg/\",\"expires_in\":3600,\"refresh_token\":\"873e06747d964a0d80f79181c98aceac\"};        $ httpBackend.when('POST',window.apiUrls.refresh_token).respond(refreshToken);
        $ httpBackend.when('POST',window.apiUrls.login).respond(mockLogin);
    }));    它('的UserManager应在2秒后刷新令牌,注入(功能(的UserManager,$ httpBackend){
        UserManager.oauth.Login('GTEST','qweqwe');
        $ httpBackend.flush();
        等待(4000);
        $ httpBackend.flush();
        expect(UserManager.oauth.credentials.access_token).toEqual('ef235de46dba53ba69ed049f57496ec902da5d28AAFB1HdeTE-2vwnhn0s-nUFtoGtj9rSm9waiuhySCpFJxKVYqOx9W7Gt');
    }));
});


解决方案

有一个关于这个讨论的此处。 既然承诺是异步你需要做的$ rootScope。$摘要()在测试中,让他们去

在添加此您$ httpBackend.flush():

 如果(!$ rootScope。$$阶段){
    $ rootScope $适用()。
}

所以你的code变为:

 它('的UserManager应在2秒后刷新令牌,注入(功能(的UserManager,$ httpBackend){
    UserManager.oauth.Login('GTEST','qweqwe');    如果(!$ rootScope。$$阶段){
        $ rootScope $适用()。
    }    $ httpBackend.flush();
    expect(UserManager.oauth.credentials.access_token).toEqual('ef235de46dba53ba69ed049f57496ec902da5d28AAFB1HdeTE-2vwnhn0s-nUFtoGtj9rSm9waiuhySCpFJxKVYqOx9W7Gt');
}));

My UserManager service automatically fires a $http POST every hour to refresh the user access token.

I'm trying to mock that call to verify that the token is being refreshed, but when I try to flush the $httpbackend I get an error saying 'No pending requests to flush' even though I know that the refresh function has been called (added a console.log just to verify).

Either the fact that the function is being called through a setTimeOut is effecting the $httpbackend or I'm missing something else.

Code attached bellow:

describe("UserManager Service Testing", function() {

    beforeEach(angular.mock.module('WebApp'));

    beforeEach(inject(function($httpBackend) {
        window.apiUrls = jQuery.parseJSON('{"cover_art": "http://myrockpack.com/ws/cover_art/?locale=en-us", "channel_search_terms": "http://myrockpack.com/ws/complete/channels/?locale=en-us", "register": "http://myrockpack.com/ws/register/", "categories": "http://myrockpack.com/ws/categories/?locale=en-us", "reset_password": "http://myrockpack.com/ws/reset-password/", "share_url": "http://myrockpack.com/ws/share/link/", "video_search": "http://myrockpack.com/ws/search/videos/?locale=en-us", "channel_search": "http://myrockpack.com/ws/search/channels/?locale=en-us", "video_search_terms": "http://myrockpack.com/ws/complete/videos/?locale=en-us", "popular_channels": "http://myrockpack.com/ws/channels/?locale=en-us", "popular_videos": "http://myrockpack.com/ws/videos/?locale=en-us", "login": "http://myrockpack.com/ws/login/", "login_register_external": "http://myrockpack.com/ws/login/external/", "refresh_token": "http://myrockpack.com/ws/token/"}');
        var mockLogin = {"token_type":"Bearer","user_id":"oCRwcy5MRIiWmsJjvbFbHA","access_token":"752a4f939662846a787a1474ad17ffddcd816dc7AAFB1G7HvgH-0qAkcHMuTESIlprCY72xWxyiuhySCpFJxKVYqOx9W7Gt","resource_url":"http://myrockpack.com/ws/oCRwcy5MRIiWmsJjvbFbHA/","expires_in":2,"refresh_token":"fa2f47f3590240e4bdfdbde03bf8042d"}
        var refreshToken = {"token_type":"Bearer","user_id":"CeGfSz6dQW2ga2P2tKb3Bg","access_token":"ef235de46dba53ba69ed049f57496ec902da5d28AAFB1HdeTE-2vwnhn0s-nUFtoGtj9rSm9waiuhySCpFJxKVYqOx9W7Gt","resource_url":"http://myrockpack.com/ws/CeGfSz6dQW2ga2P2tKb3Bg/","expires_in":3600,"refresh_token":"873e06747d964a0d80f79181c98aceac"};

        $httpBackend.when('POST', window.apiUrls.refresh_token).respond(refreshToken);
        $httpBackend.when('POST', window.apiUrls.login).respond(mockLogin);
    }));

    it('UserManager should refresh the token after 2 seconds', inject(function(UserManager, $httpBackend) {
        UserManager.oauth.Login('gtest','qweqwe');
        $httpBackend.flush();
        waits(4000);
        $httpBackend.flush();
        expect(UserManager.oauth.credentials.access_token).toEqual('ef235de46dba53ba69ed049f57496ec902da5d28AAFB1HdeTE-2vwnhn0s-nUFtoGtj9rSm9waiuhySCpFJxKVYqOx9W7Gt');
    }));
});

解决方案

There is a discussion about this here. "Since promises are async you need to do $rootScope.$digest() in your tests to get them going"

add this before your $httpBackend.flush():

if(!$rootScope.$$phase) {
    $rootScope.$apply();
}

so your code becomes:

it('UserManager should refresh the token after 2 seconds', inject(function(UserManager, $httpBackend) {
    UserManager.oauth.Login('gtest','qweqwe');

    if(!$rootScope.$$phase) {
        $rootScope.$apply();
    }

    $httpBackend.flush();
    expect(UserManager.oauth.credentials.access_token).toEqual('ef235de46dba53ba69ed049f57496ec902da5d28AAFB1HdeTE-2vwnhn0s-nUFtoGtj9rSm9waiuhySCpFJxKVYqOx9W7Gt');
}));

这篇关于Angularjs测试(茉莉花) - $ HTTP返回“不挂起请求刷新”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆