程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了为什么我会收到错误…意外请求:GET / internalapi / quotes大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决为什么我会收到错误…意外请求:GET / internalapi / quotes?

开发过程中遇到为什么我会收到错误…意外请求:GET / internalapi / quotes的问题如何解决?下面主要结合日常开发的经验,给出你关于为什么我会收到错误…意外请求:GET / internalapi / quotes的解决方法建议,希望对你解决为什么我会收到错误…意外请求:GET / internalapi / quotes有所启发或帮助;

您需要告诉$ httpBACkend期待GET请求。

describe("Myservice", function () {

   beforeEach(module('MyApp'));
   beforeEach(module("restangular"));

   var Restangular, ms;

    beforeEach(inject(function (_Restangular_, MyservicE) {
        ms = Myservice;

        Restangular = _Restangular_;
    }));


    it("retrIEveQuotes should be defined", function () {
        expect(ms.retrIEveQuotes).toBedefined();
    });

    it("retrIEveQuotes should return array of quotes", inject(function ($httpBACkend) {

        $httpBACkend.whenGET("internalAPI/quotes").respond({ Hello: 'World' });

        //expect a get request to "internalAPI/quotes"
        $httpBACkend.expectGET("internalAPI/quotes");

        ms.retrIEveQuotes();
        $httpBACkend.flush();
    }));

});

或者,您可以穿上自己respond()的衣服expectGET()。我更喜欢whenGET()beforeEach()那种方式陈述我的陈述,而不必在每个测试中都定义响应。

        //expect a get request to "internalAPI/quotes"
        $httpBACkend.expectGET("internalAPI/quotes").respond({ Hello: 'World' });

        ms.retrIEveQuotes();
        $httpBACkend.flush();

解决方法

我在我的角度应用程序中定义了以下服务:

services.factory('Myservice',['Restangular',function (Restangular) {
       return {
           events : { loading : true },retrieveQuotes : function() {
               return Restangular.all('quotes').getList().then(function() {
                   return { Hello: 'World' };
               });
           }
    };
}]);

我正在编写以下规范对其进行测试:

describe("Myservice",function () {

    beforeEach(module('MyApp'));
    beforeEach(module("restangular"));

    var $httpBACkend,Restangular,ms;

    beforeEach(inject(function (_$httpBACkend_,_Restangular_,MyservicE) {
        ms = Myservice;
        $httpBACkend = _$httpBACkend_;
        Restangular = _Restangular_;
    }));


    it("retrieveQuotes should be defined",function () {
        expect(ms.retrieveQuotes).toBeDefined();
    });

    it("retrieveQuotes should return array of quotes",function () {

        $httpBACkend.whenGET("internalapi/quotes").respond({ Hello: 'World' });
        ms.retrieveQuotes();
        $httpBACkend.flush();
    });

});

每当我运行测试时,第一个测试通过,但第二个测试产生错误:

Error: Unexpected request: GET /internalapi/quotes

我究竟做错了什么?

编辑:

原来,我已经配置Restangular是这样的&Hellip;
RestangularProvider.setBaseUrl("/internalapi");。但是我在打电话给我internalapi/quotes。注意缺少“
/”。一旦我添加了斜线,/internalapi/quotes一切都很好:)

@H_262_60@@H_262_60@
@H_262_60@

大佬总结

以上是大佬教程为你收集整理的为什么我会收到错误…意外请求:GET / internalapi / quotes全部内容,希望文章能够帮你解决为什么我会收到错误…意外请求:GET / internalapi / quotes所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。