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

AngularJs Jasmine 單元測試中的 $httpBackend

$httpBackend in AngularJs Jasmine unit test(AngularJs Jasmine 單元測試中的 $httpBackend)
本文介紹了AngularJs Jasmine 單元測試中的 $httpBackend的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我無法讓我的單元測試正常工作.我有一個 $scope 數(shù)組,它一開始是空的,但應該用 $http.get() 填充.在真實環(huán)境中,數(shù)組中大約有 15 個左右的對象,但對于我的單元測試,我只抓了 2 個.對于單元測試,我有:

I'm unable to get my unit test to work properly. I have a $scope array that starts out empty, but should be filled with an $http.get(). In the real environment, there'd be approx 15 or so objects in the array, but for my unit test I just grabbed 2. For the unit test, I have:

expect($scope.stuff.length).toBe(2);

但是 jasmine 的錯誤是:Expected 0 to be 2.

But jasmine's error is: Expected 0 to be 2.

這是我的 controller.js:

here's my controller.js:

$scope.stuff = [];
$scope.getStuff = function () {
    var url = site.root + 'api/stuff';
    $http.get(url)
        .success(function (data) {
            $scope.stuff = data;
        })
        .error(function(error) {
            console.log(error);
        });
};

而我的 controller.spec.js 是:

and my controller.spec.js is:

/// <reference path="../../../scripts/angular-loader.min.js" />
/// <reference path="../../../scripts/angular.min.js" />
/// <reference path="../../../scripts/angular-mocks.js" />
/// <reference path="../../../scripts/angular-resource.js" />
/// <reference path="../../../scripts/controller.js" />
beforeEach(module('ui.router'));
beforeEach(module('ngResource'));
beforeEach(module('ngMockE2E'));
beforeEach(module('myApplication'));
var $scope;
var controller;
var httpLocalBackend;

beforeEach(inject(function ($rootScope, $controller, $injector) {
    $scope = $rootScope.$new();
    controller = $controller("StuffController", {
        $scope: $scope
    });
}));

beforeEach(inject(function ($httpBackend) {
    httpLocalBackend = $httpBackend;
}));

it('should get stuff', function () {
    var url = '/api/stuff';
    var httpResponse = [{ "stuffId": 1 }, { "stuffId": 2 }];
    httpLocalBackend.expectGET(url).respond(200, httpResponse);
    $scope.getStuff();
    expect($scope.stuff.length).toBe(2);
    //httpLocalBackend.flush();
} );

現(xiàn)在,很明顯,我更改了變量名稱等,因為這是為了工作,但希望這對任何人都有足夠的信息來幫助我.如果需要,我可以提供更多.取消注釋 .flush() 行時,我還會遇到第二個錯誤,但稍后我會談到.

Now, obviously, I changed variable names and such since this is for work, but hopefully this is enough information for anybody to help me. I can provide more if needed. I also get a second error when uncommenting the .flush() line, but I'll get to that a bit later.

非常感謝任何幫助,并在此先感謝!

Any help is greatly appreciated and thanks in advance!

終于搞定了!這是我的最終代碼:

Finally got it working! Here's my final code:

it('should get stuff', function () {
    var url = '/api/stuff';
    var httpResponse = [{ "stuffId": 1 }, { "stuffId": 2 }];
    httpLocalBackend.expectGET(url).respond(200, httpResponse);
    $scope.getStuff();
    httpLocalBackend.flush();
    expect($scope.stuff.length).toBe(2);
} );

編輯 2:我遇到了另一個問題,我認為這可能是其根本原因.請參閱在啟動時調用函數(shù)時單元測試失敗

EDIT 2: I ran into another problem, which I think may have been the root cause of this. See Unit test failing when function called on startup

推薦答案

需要在expect($scope.stuff.length).toBe(2)之前放置httpLocalBackend.flush()語句.發(fā)出請求后,您需要刷新它以使數(shù)據(jù)在您的客戶端代碼中可用.

You need to place httpLocalBackend.flush() statement before expect($scope.stuff.length).toBe(2). Once you make a request you need to flush it for the data to be available in your client code.

it('should get stuff', function () {
    var url = '/api/roles';
    var httpResponse = [{ "stuffId": 1 }, { "stuffId": 2 }];
    scope.getStuff(); //Just moved this from after expectGET
    httpLocalBackend.expectGET(url).respond(200, httpResponse);
    httpLocalBackend.flush();
    expect($scope.stuff.length).toBe(2);
} );

試試這個.

這篇關于AngularJs Jasmine 單元測試中的 $httpBackend的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

How can I get my jasmine tests fixtures to load before the javascript considers the document to be quot;readyquot;?(在 javascript 認為文檔“準備好之前,如何讓我的 jasmine 測試裝置加載?) - IT屋-程序員軟件開發(fā)技術
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 - 如何監(jiān)視函數(shù)中的函數(shù)調用?)
主站蜘蛛池模板: 亚洲欧美少妇 | 99久久久国产精品 | 少妇久久久 | 国产激情网 | 成人福利影院 | 日韩免费一区二区 | 中文字幕在线观看www | 亚洲成人av | 欧美激情99 | 日韩免费高清视频 | 四虎影院在线观看av | 国产 日韩 欧美 中文 在线播放 | 欧美色视频免费 | 日本久久网 | 国产精品视频久久久 | 色婷婷一区二区三区四区 | 国产精品久久久久久久久久 | 亚洲福利网站 | 亚洲一区二区视频 | 国产极品粉嫩美女呻吟在线看人 | 婷婷综合| caoporon| 欧美不卡一区二区 | 一区二区三区四区免费在线观看 | 亚洲一区二区三 | 国产 亚洲 网红 主播 | 亚洲国产精品久久久久 | 91精品久久久久久久久中文字幕 | 九九热在线免费视频 | 欧美日韩激情 | 一级黄色片在线免费观看 | 久操伊人 | 日本三级精品 | 在线一区观看 | 毛片韩国| 亚洲区在线 | 欧美一区二区成人 | 激情毛片 | 在线一区二区三区 | 国产乱码精品一区二区三区中文 | 青青草免费在线视频 |