使用$ httpBackend + $资源角度人缘单元测试不与模块自举 [英] angular karma unit test using $httpBackend + $resource without bootstrapping with module

查看:286
本文介绍了使用$ httpBackend + $资源角度人缘单元测试不与模块自举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要求


  • 角基于REST的客户端组件

  • 必须私有API和服务
  • 之间的互动
  • 噶/茉莉花测试不引导应用模块

目前的窘境

我们不希望引导单元测试整个应用程序(使用模块()或angular.mock.module())。它似乎不可持续的一个企业级应用,尽管角的组件注册是一个非常有用的功能。我发现所有的例子做这种方式,每次我们目前的业务目标不过,我需要做的是无自举程序模块。

到目前为止,我已经成功注入既$资源和$ httpBackend,他们是功能性。我可以指定一个响应行为到$ httpBackend对象,我可以注入$资源到在测试套件的RESTClient实现工厂。然而,当我尝试运行它,它看来,他们不知道彼此,因此客户端实际发送呼叫(不良#1),留下$ httpBackend对象以优秀的预期并没有什么等待刷新(#2)

请帮忙

您能指出我的思念让这些两件与出调用模块一起工作的配置错误()?

测试误差

 错误:没有挂起请求刷新!
        。在功能$ httpBackend.flush(node_modules /角嘲笑/角mocks.js:1544:34)
        在对象<&匿名GT; (/var/folders/8h/lqyqq1ks6vv8z5mpsxjqn5sw0000gn/T/89bece8e9480f44013f53b3f1b05b4ba.browserify:855:22< - SRC /应用程序/服务/ restClient_test.js:24:0)
错误:预约等待人数:GET https://ourlink.nl
        。在功能$ httpBackend.verifyNoOutstandingExpectation(node_modules /角嘲笑/角mocks.js:1577:13)
        在对象<&匿名GT; (/var/folders/8h/lqyqq1ks6vv8z5mpsxjqn5sw0000gn/T/89bece8e9480f44013f53b3f1b05b4ba.browserify:847:22< -

从失败的RESTClient实现XHR

浏览器错误

  XMLHtt prequest无法加载undefinedhttps://oursite.nl用户id = UserZ?跨起源请求只支持协议方案:HTTP,数据,镀铬,镀铬的扩展,HTTPS,镀铬的扩展资源。

restClient.js:

 函数RESTClient实现($​​资源的baseUrl){
    使用严格的;    返回{
        得到:得到
    };    函数的get(路线选项){
        VAR自我=这一点;
        无功负载= $资源(self.baseUrl +路线,期权)获得(options.routeParams);
        返回的有效载荷;
    }
}module.exports = RESTClient实现;

restClient_test.js:

 描述('RESTClient实现',函数(){
    VAR的客户端,$ httpBackend;    beforeEach(注(功能(_ $ httpBackend_){
        $ httpBackend = _ $ httpBackend_;
    }));    beforeEach(函数(){
        变量$ =喷油angular.injector(['NG','ngResource']);
        this.client =要求('./ restClient.js');
        客户端= this.client($ injector.get('$资源'));
    });    afterEach(函数(){
        $ httpBackend.verifyNoOutstandingExpectation();
        $ httpBackend.verifyNoOutstandingRequest();
    });    它('发送一个GET请求',函数(){
        $ httpBackend.expectGET('https://oursite.nl')
            .respond('200',{用户名:UserZ'},{令牌:'1234'});
        client.get('https://oursite.nl',{用户名:UserZ'});
        $ httpBackend.flush();
    });
});


解决方案

如果不使用ngMock茉莉花/摩卡佣工它必须是这样的:

 描述('RESTClient实现',函数(){
    变量$喷油器,客户端,$ httpBackend;    beforeEach(函数(){
        $喷油器= angular.injector(['NG','ngResource','ngMock']);
        $ httpBackend = $ injector.get('$ httpBackend');
        this.client =要求('./ restClient.js');
        客户端= this.client($ injector.get('$资源'));
    });
    ...
});

Requirements

  • Angular based REST client component
  • Must interact between a private API and services
  • Karma/Jasmine test does not bootstrap app module

Current dilemma

We do not want to bootstrap the entire application for unit tests (using module() or angular.mock.module()). It does not seem sustainable for an enterprise level application, even though Angular's component registration is a useful feature. All the examples I have found do it this way, however per our current business objectives, I need to do it without bootstrapping the app module.

So far I have successfully injected both $resource and $httpBackend and they are functional. I can assign a respond behavior to the $httpBackend object, and I can inject $resource into the restClient factory in the test suite. However when I try to run it, it appears that they are not aware of each other, and therefore the client is actually sending calls (undesirable #1), leaving the $httpBackend object waiting with outstanding expectations and nothing to flush (#2).

Please help

Can you point out the configuration errors I'm missing to get these two pieces to work together with out calling module()?

Testing errors

Error: No pending request to flush !
        at Function.$httpBackend.flush (node_modules/angular-mocks/angular-mocks.js:1544:34)
        at Object.<anonymous> (/var/folders/8h/lqyqq1ks6vv8z5mpsxjqn5sw0000gn/T/89bece8e9480f44013f53b3f1b05b4ba.browserify:855:22 <- src/app/services/restClient_test.js:24:0)
Error: Unsatisfied requests: GET https://ourlink.nl
        at Function.$httpBackend.verifyNoOutstandingExpectation (node_modules/angular-mocks/angular-mocks.js:1577:13)
        at Object.<anonymous> (/var/folders/8h/lqyqq1ks6vv8z5mpsxjqn5sw0000gn/T/89bece8e9480f44013f53b3f1b05b4ba.browserify:847:22 <-

Browser error from failed restClient XHR

XMLHttpRequest cannot load undefinedhttps://oursite.nl?userId=UserZ. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.

restClient.js:

function restClient ($resource, baseUrl) {
    'use strict';

    return {
        get: get
    };

    function get (route, options) {
        var self = this;
        var payload = $resource(self.baseUrl + route, options).get(options.routeParams);
        return payload;
    }
}

module.exports = restClient;

restClient_test.js:

describe('restClient', function () {
    var client, $httpBackend;

    beforeEach(inject(function(_$httpBackend_) {
        $httpBackend = _$httpBackend_;
    }));

    beforeEach(function () {
        var $injector = angular.injector(['ng', 'ngResource']);
        this.client = require('./restClient.js');
        client = this.client($injector.get('$resource'));
    });

    afterEach(function() {
        $httpBackend.verifyNoOutstandingExpectation();
        $httpBackend.verifyNoOutstandingRequest();
    });

    it('sends a get request', function () {
        $httpBackend.expectGET('https://oursite.nl')
            .respond('200', {userId: 'UserZ'}, {token: '1234'});
        client.get('https://oursite.nl', {userId: 'UserZ'});
        $httpBackend.flush();
    });
});

解决方案

Without using ngMock Jasmine/Mocha helpers it has to be something like this:

describe('restClient', function () {
    var $injector, client, $httpBackend;

    beforeEach(function () {
        $injector = angular.injector(['ng', 'ngResource', 'ngMock']);
        $httpBackend = $injector.get('$httpBackend');
        this.client = require('./restClient.js');
        client = this.client($injector.get('$resource'));
    });
    ...
});

这篇关于使用$ httpBackend + $资源角度人缘单元测试不与模块自举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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