如何在茉莉花中处理异步HTTP请求

时间:2019-03-01 11:03:58

标签: angularjs jasmine karma-jasmine

'use strict';

describe('component test : add', function () {

    // add module reference you want to test
    beforeEach(module('addView'));

    // add templates [from karma]
    beforeEach(module('templates'));

    var element;
    var scope;
    var $httpBackend;
    var template;
    var res = [
        {
            id: 3,
            columnname: 'toefl'
        }
    ];
    var $rootScope;


    beforeEach(inject(function (_$rootScope_, $compile, _$httpBackend_) {
        $httpBackend = _$httpBackend_;
        $rootScope = _$rootScope_;

        $httpBackend.when('GET', 'http://localhost:51275/api/ExtraColumn')
            .respond(200, res);

        $httpBackend.when('GET', 'http://localhost:51275/api/Interviewee')
            .respond(200, res);

        scope = $rootScope.$new();

        element = angular.element('<add-view></add-view>');
        scope.$apply(function () {
            template = $compile(element)(scope);
        });

        $httpBackend.flush();
    }));


    // tests

    it('extra column http get', function () {
        $httpBackend.expectGET('http://localhost:51275/api/ExtraColumn');

    });

    it('interviewees http get', function () {
        $httpBackend.expectGET('http://localhost:51275/api/Interviewee');      
    });

    it('header text', function () {
        var title = element.find('h1');
        expect(title.text()).toContain('Add a new interviewee');
    });

    it('form validation', function () {
        expect(element.isolateScope().intervieweeFrom.$valid).toBe(false);
    });
});

这里提供了上面的测试代码。我使用业力和茉莉花来测试我的angularjs代码。 如何处理这两个http get请求? api / ExtraColumn应该在api /受访者之前完成。我是单元测试的新手。您能给我一个基本的例子吗?谢谢

0 个答案:

没有答案