多个意外请求:GET

问题描述 投票:0回答:1

我有一个 Angular 应用程序,在初始化时会发出多个 http 请求。

我已经设置了一个测试,以期待第一个请求和第二个请求,

describe("MyController--", function () {

    var controller, scope, $httpBackend, myFactory;

    var iResponse = {
        table: 'red',
        price: '1.99'
    };

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

    beforeEach(inject(function (_$controller_, _$rootScope_, _$httpBackend_, _myFactory_) {
        scope = _$rootScope_.$new();
        $httpBackend = _$httpBackend_;

        $httpBackend.expectGET("/app/shopping/cart/layout.html").respond({});
        $httpBackend.expectGET("/app/rest/products").respond(iResponse);

        myFactory = _myFactory_;
        spyOn(myFactory, 'getData').and.callThrough();

        controller = _$controller_('MainController', {$scope: scope});
        scope.$apply();
        $httpBackend.flush();
    }));

    it('should verify that the controller exists ', function() {
        expect(controller).toBeDefined();
    });

通过上述操作,我不断看到错误:

Error: Unexpected request: GET /app/rest/products
Information:    Expected GET /app/shopping/cart/layout.html

我缺少什么想法吗?

angularjs jasmine karma-runner karma-jasmine
1个回答
0
投票

首先,我会在 Karma conf 中预加载 HTML,这样您就不必期待 HTML。

其次,您的控制器是否调用了意外的 URL?如果是这样,您应该期待这个请求。

© www.soinside.com 2019 - 2024. All rights reserved.