久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Angular 資源測試:$httpBackend.flush() 導致意外請求

Angular resource testing: $httpBackend.flush() cause Unexpected request(Angular 資源測試:$httpBackend.flush() 導致意外請求)
本文介紹了Angular 資源測試:$httpBackend.flush() 導致意外請求的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我想測試 angularjs 資源.

I want to test angularjs resource.

'use strict';

/**
 * AddressService provides functionality to use address resource in easy way.
 *
 * This is an example usage of method:
 *
 * `get`:
 *
     var a = AddressService.get({id: '1'},
         function (data) {
            // Work here with your resource
         }
     );
 *
 */
App.factory('AddressService', function ($resource, $rootScope) {
    var url = [
            $rootScope.GLOBALS.API_PATH,
            $rootScope.GLOBALS.API_VERSION,
            'models/address/:id'
        ].join('/'),

        actions = {
            'get': {
                method: 'GET',
                params: {id: '@id'}
            }
        };
    return $resource(url, {}, actions);
});

我創建了測試:

'use strict';

var $httpBackend;

describe('Service: AddressService', function () {

    beforeEach(module('myApp'));

    beforeEach(inject(function ($injector) {
        var url_get = 'api/v1/models/address/5';

        var response_get = {
            address_line_1: '8 Austin House Netly Road',
            address_line_2: 'Ilford IR2 7ND'
        };

        $httpBackend = $injector.get('$httpBackend');

        $httpBackend.whenGET(url_get).respond(response_get);

    }));

    describe('AddressService get test', function () {
        it('Tests get address', inject(function (AddressService) {
            var address = AddressService.get({ id: '5'});
            $httpBackend.flush();
            expect(address.address_line_1).toEqual('8 Austin House Netly Road');
            expect(address.address_line_2).toEqual('Ilford IR2 7ND');
        }));
    });
});

我對角度的經驗不是很好.我在 karma.config.js 中設置了 jasmine.AngularJS v1.0.6Yoeman 和 Grunt 管理項目.

I am not experienced with angular very well. I have set jasmine in karma.config.js. AngularJS v1.0.6 Yoeman and Grunt manage project.

我試試 grunt test

Running "karma:unit" (karma) task
INFO [karma]: Karma server started at http://localhost:8080/
INFO [launcher]: Starting browser Chrome
INFO [Chrome 27.0 (Linux)]: Connected on socket id RFFUY5bW8Hb5eTu0n-8L
Chrome 27.0 (Linux) Service: AddressService AddressService get Tests get address FAILED
        Error: Unexpected request: GET views/home.html
        No more request expected
            at Error (<anonymous>)
            at $httpBackend (/home/bart/y/projects/x/frontend/app/components/angular-mocks/angular-mocks.js:910:9)
            at sendReq (/home/bart/y/projects/x/frontend/app/components/angular/angular.js:9087:9)
            at $http (/home/bart/y/projects/x/frontend/app/components/angular/angular.js:8878:17)
            at Function.$http.(anonymous function) [as get] (/home/bart/y/projects/x/frontend/app/components/angular/angular.js:9021:18)
            at $q.when.then.then.next.locals (/home/bart/y/projects/x/frontend/app/components/angular/angular.js:7394:34)
            at wrappedCallback (/home/bart/y/projects/x/frontend/app/components/angular/angular.js:6797:59)
            at wrappedCallback (/home/bart/y/projects/x/frontend/app/components/angular/angular.js:6797:59)
            at /home/bart/y/projects/x/frontend/app/components/angular/angular.js:6834:26
            at Object.Scope.$eval (/home/bart/y/projects/x/frontend/app/components/angular/angular.js:8011:28)
        Error: Declaration Location
            at window.jasmine.window.inject.angular.mock.inject (/home/bart/y/projects/x/frontend/app/components/angular-mocks/angular-mocks.js:1744:25)
            at null.<anonymous> (/home/bart/y/projects/x/frontend/test/spec/services/userdata/AddressService.js:32:30)
            at null.<anonymous> (/home/bart/y/projects/x/frontend/test/spec/services/userdata/AddressService.js:31:5)
            at /home/bart/y/projects/x/frontend/test/spec/services/userdata/AddressService.js:5:1
..................................................................
Chrome 27.0 (Linux): Executed 67 of 67 (1 FAILED) (0.343 secs / 0.179 secs)
Warning: Task "karma:unit" failed. Use --force to continue.

如果我在測試中刪除 $httpBackend.flush().測試通過.但我從 address 得到 undefined.我看到了例子: example 都使用了 flush() 但只有我得到愚蠢的異常:錯誤:意外請求:GET views/home.html

If I remove $httpBackend.flush() in test. Test is passing. But I am getting undefined from address. I saw examples: example all use the flush() But only I get silly exception: Error: Unexpected request: GET views/home.html

views/home.html 是我在項目目錄中的視圖.我不知道如何解決我的問題我找不到任何解決方案.我是否以某種方式錯過了重點?

views/home.html is my view in project directory. I have no idea how solve my problem I could not find any solution. Am I missing the point somehow?

任何人都可以看到我的代碼中的錯誤嗎?所有建議將不勝感激.

Can anybody see mistake in my code? All suggestions will be appreciate.

編輯

我發現我需要使用這個:

I have found that I need to use this:

$httpBackend.when('GET', /.html$/).passThrough();

但另一個問題是我得到了:

But another problem is I am getting:

TypeError: Object #<Object> has no method 'passThrough'

推薦答案

終于找到解決方案了:

我試圖遵循這個:其他帖子

所以我添加了 $templateCache:

'use strict';

var $httpBackend;

describe('Service: AddressService', function () {

    beforeEach(module('myApp'));

    beforeEach(inject(function ($injector, $templateChache) {
        $templateCache.put('views/home.html', '.<template-goes-here />');
        var url_get = 'api/v1/models/address/5';

        var response_get = {
            address_line_1: '8 Austin House Netly Road',
            address_line_2: 'Ilford IR2 7ND'
        };

        $httpBackend = $injector.get('$httpBackend');

        $httpBackend.whenGET(url_get).respond(response_get);

    }));

    describe('AddressService get test', function () {
        it('Tests get address', inject(function (AddressService) {
            var address = AddressService.get({ id: '5'});
            $httpBackend.flush();
            expect(address.address_line_1).toEqual('8 Austin House Netly Road');
            expect(address.address_line_2).toEqual('Ilford IR2 7ND');
        }));
    });
});

這篇關于Angular 資源測試:$httpBackend.flush() 導致意外請求的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I get my jasmine tests fixtures to load before the javascript considers the document to be quot;readyquot;?(在 javascript 認為文檔“準備好之前,如何讓我的 jasmine 測試裝置加載?) - IT屋-程序員軟件開發技術
What do jasmine runs and waitsFor actually do?(jasmine 運行和等待實際上是做什么的?)
How to provide mock files to change event of lt;input type=#39;file#39;gt; for unit testing(如何提供模擬文件來更改 lt;input type=filegt; 的事件用于單元測試)
How to unit test a chained method using Jasmine(如何使用 Jasmine 對鏈式方法進行單元測試)
How do I inject $rootScope into an AngularJS unit test?(如何將 $rootScope 注入 AngularJS 單元測試?)
Jasmine - How to spy on a function call within a function?(Jasmine - 如何監視函數中的函數調用?)
主站蜘蛛池模板: 性一区| 国产成人综合久久 | 日韩精品一区二区久久 | 一级免费毛片 | 婷婷色国产偷v国产偷v小说 | 国产亚洲精品精品国产亚洲综合 | 成人免费网站在线 | 日韩精品一区二区三区老鸭窝 | 中文字幕第一页在线 | 国产高清视频一区 | 国产精品中文在线 | 搞黄视频免费看 | 九色国产 | 中文字幕一区二区不卡 | 97国产成人 | 天天色天天色 | 久久精品网 | 欧美久久一区二区 | 日韩中文字幕一区 | 国产成人免费视频网站视频社区 | 精品一区国产 | 久久天天 | 亚洲免费在线观看视频 | 成人做爰9片免费看网站 | 国产精品99久久久精品免费观看 | 国产成人免费视频 | 日韩二三区 | 欧美综合久久久 | 久久久久九九九女人毛片 | 国产原创在线观看 | 日日噜 | 国产精品成人品 | 69av网| 欧美乱码精品一区二区三区 | 成人在线一级片 | 日韩区 | 一级做受毛片免费大片 | 精品欧美 | 成人小视频在线免费观看 | 中文字幕在线看第二 | 久久久久久国产精品免费免费狐狸 |