多个意外请求:GET

时间:2016-02-29 17:20:57

标签: angularjs jasmine karma-runner karma-jasmine

我有一个有角度的应用程序,在初始化时,会发出一些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

我缺少什么想法?

1 个答案:

答案 0 :(得分:0)

首先我会在karma conf中预加载HTML,所以你不必期待HTML

其次,您的控制器是否调用了意外的网址?如果是这样,你就会期待这个请求

一切顺利