角度单元测试失败,错误:意外请求:GET

时间:2014-10-02 17:44:24

标签: angularjs unit-testing angular-services

我是angularjs测试的新手并且一直在尝试运行此测试,但它一次又一次地失败并出现同样的错误。我在这里查看了问题并阅读了文档,但没有找到导致此错误的原因。

非常感谢帮助。提前谢谢。

我的service.js

'use strict';

var services = angular.module('services',['ngResource'])

services.factory('callAppsList',['$resource',function($resource){
    return $resource('/api/apps/:appId', {}, {
        query: {method:'GET', isArray:false},
        get: {method:'GET', isArray:false},
    });
}])

serviceSpec.js

//serviceSpec testing

describe("Testing service", function() {

    beforeEach(module('services'));

    var service, $httpBackend, response;
    var url = 'http://0.0.0.0:5000/api/apps/a365cc3520c7a70a553e95ee354670264'


     beforeEach(inject(function( _$httpBackend_, callAppsList) {
        $httpBackend = _$httpBackend_;
        res = { msg :'name'};

        service = callAppsList;

    }));

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

    //tests

    it("callAppsList should be defined", function () {
        expect(service).toBeDefined();
    });


    it('should send a ping and return the response',(function () {

        res = service.get({appId: 'a365cc3520c7a70a553e95ee354670264'});
        $httpBackend.whenGET(url).respond(res);

        $httpBackend.expectGET(url)

        $httpBackend.flush();
        //expect(res.msg).toEqual('name');

    }));

  });

第一次测试(当我测试它是否定义了传递)但下一次测试失败。

错误:

Error: Unexpected request: GET /api/apps/a365cc3520c7a70a553e95ee354670264
    Expected GET http://0.0.0.0:5000/api/apps/a365cc3520c7a70a553e95ee354670264 in /home/anurag/anurag/projects/betablide/applunge/glide/static/test/lib/angular-mocks.js (line 1179)

烧瓶服务器正在另一个终端中运行。 请让我知道我在这里做错了什么,以及如何进一步。

1 个答案:

答案 0 :(得分:1)

正如评论中所提到的,更改网址已经解决了。我还在spec文件中更改了新行。希望这可以帮助别人。

//serviceSpec testing

describe("Testing service", function() {

    beforeEach(module('services'));

    var service, $httpBackend, response;
    var url = '/api/apps/a365cc3520c7a70a553e95ee354670264'


     beforeEach(inject(function( _$httpBackend_, callAppsList) {
        $httpBackend = _$httpBackend_;    
        service = callAppsList;

    }));

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

    //tests

    it("callAppsList should be defined", function () {
        expect(service).toBeDefined();
    });


    it('should send a ping and return the response',(function () {

        res = service.get({appId: 'a365cc3520c7a70a553e95ee354670264'});
        $httpBackend.whenGET(url).respond({status: 200});
        //explicitly flushes pending requests
        $httpBackend.flush();
        expect(res.status).toEqual(200);

    }));

  });