如何避免“错误:意外的请求:GET”每一个AngularJS测试做了rootScope.digest()或httpBackend.flush时间() [英] How to avoid the 'Error: Unexpected request: GET' every time an AngularJS test does a rootScope.digest() or httpBackend.flush()

查看:275
本文介绍了如何避免“错误:意外的请求:GET”每一个AngularJS测试做了rootScope.digest()或httpBackend.flush时间()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所有的单元测试,而不是E2E测试,即做一个明确的rootScope.digest()或httpBackend.flush()来刷新异步回调,遇到错误:

 如何避免错误:意外的请求:GET
没有更多的要求预期

我认为这是因为httpBackend调用用户界面路由器模板。我不知道为什么它要这么做。我不要求这一点。我只希望它叫我嘲笑JSON服务。

此错误迫使我有下面的语句在每次()块:

  $ httpBackend.whenGET(/ \\ $ HTML /),响应('')。

必须有一个更合适的方法。

特别是如果测试没有用的$ httpBackend摆在首位:

 它('应该返回搜索的用户列表',函数(){
    //始终使用该声明,以避免从$ http服务使应用程序主要页面的请求错误
    $ httpBackend.whenGET(/ \\ $的HTML /。)响应('')。    VAR用户= NULL;
    UserService.search('TOTO',1,10,'ASC'功能(数据){
      用户= data.content;
    });
    $ rootScope $摘要()。
    期待(用户).toEqual(RESTService.allUsers.content);
  });

测试通过,但它看起来的hackish。或noobish: - )

编辑:另一项测试:

 它('应该返回的用户列表',函数(){
    //始终使用该声明,以避免从$ http服务使应用程序主要页面的请求错误
    $ httpBackend.whenGET(/ \\ $的HTML /。)响应('')。
    //创建一个模拟的请求和响应
    $ httpBackend.expectGET(ENV.NITRO_PROJECT_REST_URL +'/users/1').respond(mockedUser);
    //锻炼服务
    变种用户=无效;
    RESTService.User.get({用户名:1​​})$ promise.then(功能(数据){
      用户=数据;
    });
    //调校
    $ httpBackend.flush();
    //检查回调数据
    期待(user.firstname).toEqual(mockedUser.firstname);
    期待(user.lastname).toEqual(mockedUser.lastname);
  });


解决方案

这显然是由设计,你的测试应该检查HTTP调用正在作出,他们正在请求正确的URL。而不是检查请求是否为 / \\ $的HTML / 制成的,为什么不代替检查是否请求的正确端点做?这是否是一个指令部分或API调用。

如果你坚持扔掉,这可能是一个有用的测试,你可以将你的 whenGET() beforeEach()

All my UNIT tests, not E2E tests, that do an explicit rootScope.digest() or httpBackend.flush() to flush the asynchronous callback, experience the error:

How to avoid the 'Error: Unexpected request: GET'
No more request expected

I reckon it is because httpBackend calls the ui-router template. I don't know why it wants to do so. I'm not asking for this. I only want it to call my mocked json service.

This error forces me to have the following statement in each it() block:

$httpBackend.whenGET(/\.html$/).respond('');  

There must be a neater way.

Specially if the test has no use of the $httpBackend in the first place:

  it('should return the list of searched users', function() {
    // Always use this statement so as to avoid the error from the $http service making a request to the application main page
    $httpBackend.whenGET(/\.html$/).respond('');

    var users = null;
    UserService.search('TOTO', 1, 10, 'asc', function(data) {
      users = data.content;
    });
    $rootScope.$digest();
    expect(users).toEqual(RESTService.allUsers.content);
  });

The test passes but it looks hackish. Or noobish :-)

EDIT: Another test:

  it('should return the list of users', function () {
    // Always use this statement so as to avoid the error from the $http service making a request to the application main page
    $httpBackend.whenGET(/\.html$/).respond('');
    // Create a mock request and response
    $httpBackend.expectGET(ENV.NITRO_PROJECT_REST_URL + '/users/1').respond(mockedUser);
    // Exercise the service
    var user = null;
    RESTService.User.get({userId: 1}).$promise.then(function(data) {
      user = data;
    });
    // Synchronise
    $httpBackend.flush();
    // Check for the callback data
    expect(user.firstname).toEqual(mockedUser.firstname);
    expect(user.lastname).toEqual(mockedUser.lastname);
  });

解决方案

This is obviously by design, your tests should be checking that HTTP calls are being made and that they're requesting the correct URL. Instead of checking whether requests are made to /\.html$/ why not instead check whether requests are made to the correct endpoints? Whether that be a directives partial or an API call.

If you insist on throwing away what could be a useful test, you could move your whenGET() to a beforeEach().

这篇关于如何避免“错误:意外的请求:GET”每一个AngularJS测试做了rootScope.digest()或httpBackend.flush时间()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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