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

將本地 JSON 加載到 AngularJS 中的 Jasmine/Karma 單元測

Load local JSON into Jasmine/Karma Unit Test in AngularJS(將本地 JSON 加載到 AngularJS 中的 Jasmine/Karma 單元測試中)
本文介紹了將本地 JSON 加載到 AngularJS 中的 Jasmine/Karma 單元測試中的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我正在測試一個回調函數,它接受一個響應對象作為它的唯一參數.該對象是在其他地方發出的 HTTP 請求的響應,因此我不想在此測試中使用 $httpBackend,因為該請求與此函數無關.

I'm testing a callback function which accepts a response object as it's only parameter. This object is the response of a HTTP request made elsewhere so I don't want to user $httpBackend in this test as the request has nothing to do with this function.

它在 home.js 中,它是我的應用主頁的控制器.

It's in home.js which is a controller for the homepage of my app.

這是正在測試的函數:

 function submitLogin() {
      LoginService.login(loginPost, ctrl.username, ctrl.password, successCallback, errorCallback);
  }

// gets called in LoginService if login reponse is 201, otherwise errorCallback called
function successCallback(response) {
    // get details needed to determine correct forms for user
    var sessionData = {
      token: response.data.token,
      user_id: response.data.user_id,
      institution_name: response.data.institution_name,
      status: response.data.status,
      form_uri: getFormURI(response.data.forms) //extracts form URI for list of available forms for particular app
    };

    ctrl.formCheckInProgress = true;

    // update users forms from backend and cache them
    FormFetcherService.updateCachedForms(sessionData.user_id, sessionData.form_uri).then(function (response) {
      if (response == 'updated') {
        toastr.success('Your forms have been updated to the newest version', 'Forms Updated');
      }
      else {
        toastr.success('Your forms are already up-to-date', 'No Update     Required');
      }
    });

}

登錄服務:

    angular.module('appName').service('LoginService', ['$http', function     ($http) {
    this.login = function (url, username, password, successCallback, errorCallback) {
        var data = {
            username: username,
            password: password
        };

        $http.post(url, $.param(data), {
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
                },
                timeout: 10000
            }
        ).then(successCallback, errorCallback);
    }
}]);

我想加載一個對象來代替傳遞給函數的響應"對象.

I want to load an object which will take the place of the 'response' object being passed into the function.

有什么方法可以將 .json 文件放在我的/tests 目錄中,加載 JSON 并將其解析為 Javascript 對象,然后在我的單元測試中使用該對象?

Is there any way I could put a .json file in my /tests directory, load the JSON and parse it into a Javascript object and then use said object in my unit test?

我已經四處搜索,大多數解決方案都假設正在測試的功能中發出請求 - 這里不是這種情況.

I've searched around and most solutions assume a request is being made in the function being tested - which isn't the case here.

干杯,

院長

推薦答案

你可以這樣做:

var LoginService, $controller;

var formFetcherService = {
    updateCachedForms: jasmine.createSpy('sessionData');
}

var response = {
    data: {
        user_id: 4,
        forms: 'some'
    }
}

beforeEach(module(function($provide) {
    $provide.value('LoginService', {
        login: function(url, username, password, successCallback, errorCallback) {
            successCallback(response);
        }
    });

    $provide.value('FormFetcherService', formFetcherService);
}))

beforeEach(inject(function(_$controller_) {
    $controller = _$controller_;
});

it('should create sessionData', function() {
    var controller = $controller('HomeController');
    controller.submitLogin();
    expect(formFetcherService.updateCachedForms).toHaveBeenCalledWith(response.user_id, response.form_uri);
});

這篇關于將本地 JSON 加載到 AngularJS 中的 Jasmine/Karma 單元測試中的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 - 如何監視函數中的函數調用?)
主站蜘蛛池模板: 久久夜视频| 亚洲国产一区二区三区四区 | 人人看人人草 | 久草在线青青草 | 亚洲成人久久久 | 成人片免费看 | 五月槐花香| 久久99蜜桃综合影院免费观看 | 免费久久精品视频 | 一区在线观看 | 国产在线精品一区二区 | 日韩在线视频播放 | 国产精品资源在线 | 日本不卡免费新一二三区 | 免费视频一区 | 不卡一区二区三区四区 | 成人午夜网站 | 一级欧美一级日韩片 | 欧美精品一区免费 | 亚洲精品乱码久久久久久蜜桃 | 久在草| 亚洲人成免费 | 国产精品美女久久久久久久久久久 | 欧美亚洲高清 | 超碰激情 | 国产免费高清 | 国产精品成人69xxx免费视频 | 亚洲一区二区久久 | 日韩在线视频一区二区三区 | 久久99精品久久久久婷婷 | 中文字幕在线第一页 | 久久99网站 | 在线观看av网站 | 91伊人| www4虎| 国产精品久久久久久久久久99 | 国产精久久久 | 精品精品视频 | 一级在线免费观看 | 国产精品久久久久9999鸭 | 一级a毛片 |