错误:意外请求:POST http:// server:80 / api / metrics不再需要请求

时间:2014-07-23 16:31:02

标签: angularjs unit-testing karma-runner karma-jasmine

您好我发现了几个相关问题,并试图模仿适用于这些问题的解决方案,但我很困惑,为什么当我正确设置我的期望时会出现此错误。

我的测试

describe("Controller", function () {
var scope, SearchCtrl, httpBackend, searchSerivce;

//load the controller"s module
beforeEach(module("myApp"));

beforeEach(function () {
    angular.mock.inject(function ($injector) {
        httpBackend = $injector.get("$httpBackend");
    });
});

//initialize the controller and a mock scope
beforeEach(inject(function ($controller, $rootScope, $injector) {

    // create a new child scope
    scope = $rootScope.$new();


    scope.SearchService = $injector.get("SearchService");

    //create a new instance of basic search controller
    SearchCtrl = $controller("BasicSearchCtrl", { $scope: scope });


}));


//check the search after setting the same text
it("setting some text content in search to check it is updated in searchservice basicSearch", function () {

    scope.search = "test";

    httpBackend.when('GET', 'http://server:80/api/metrics').respond(200);

    scope.$digest();

    expect(scope.SearchService.basicSearch).toBe("test");

});

正如您所见,我已使用以下行设置此指标页面的期望

httpBackend.when('GET', 'http://server:80/api/metrics').respond(200);

我也试过

    httpBackend.expectGET('http://server:80/api/metrics');
    httpBackend.whenGET('http://server:80/api/metrics').respond(200);

并且

    httpBackend.expectGET('api/metrics');
    httpBackend.whenGET('api/metrics').respond(200);

    httpBackend.expectGET('/api/metrics');
    httpBackend.whenGET('/api/metrics').respond(200);

但他们都给我这个错误

Error: Unexpected request: POST http://server:80/api/metrics
No more request expected

任何指针都将非常感谢!

更新

解决方案......这不是一个POST

    //set up an expectation and response for this POST
    httpBackend.expectPOST('http://server:80/api/metrics').respond(200);

工作

0 个答案:

没有答案