将本地 JSON 加载到 AngularJS 中的 Jasmine/Karma 单元测

时间:2023-04-30
本文介绍了将本地 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模板网!

上一篇:量角器单击元素数组 下一篇:Webdriver JS测试期间无法切换窗口

相关文章

最新文章