gpt4 book ai didi

javascript - AngularJS 类型错误 : 'undefined' is not an object' Jasmine test

转载 作者:行者123 更新时间:2023-11-29 21:24:44 26 4
gpt4 key购买 nike

在我的测试中模拟/监视 API 调用时,我在运行 Jasmine 测试时遇到上述错误。这是我调用 API 的代码:

UploadedReleasesController.$inject = ['$log', '$scope', '$filter', '$modal', 'ReleaseService', 'TrackService', 'APP_CONFIG', 'DeliveriesService'];
function UploadedReleasesController ($log, $scope, $filter, $modal, releaseService, trackService, APP_CONFIG, deliveriesService){
.
.
.
releaseService.releases(vm.currentWorkspace).then(function (responseValues) {
vm.albums = responseValues;
if(vm.albums.length !== 0){
vm.selected.album = vm.albums[0];

其中 releaseService 是我定义的服务,releases 是我想监视的方法之一。它需要一个字符串参数,比方说 'HIGH'。这是我的测试文件,我想在其中模拟该调用:

describe('app module', function() {
var vm, scope, modalInstance, releaseService, trackService, deliveriesService;

beforeEach(module('app.uploadedReleases')); // Main module name

beforeEach(module('app.config'));
beforeEach(module('auth'));

beforeEach(function() {
var mockReleasesData = {
"test" : 100
}
};

releaseService = jasmine.createSpyObj("releaseService", ["releases"]);
releaseService.releases('HIGH').and.returnValue(mockReleasesData);
});


beforeEach(inject(function($controller, $log, $rootScope, $filter, APP_CONFIG) {

scope = $rootScope.$new();

modalInstance = {
close: jasmine.createSpy('modalInstance.close'),
open: jasmine.createSpy('modalInstance.open'),
dismiss: jasmine.createSpy('modalInstance.dismiss'),
result: {
then: jasmine.createSpy('modalInstance.result.then')
}
};

vm = $controller('UploadedReleasesController', {'APP_CONFIG':APP_CONFIG, '$log':$log, '$scope':scope, '$filter':$filter, '$modal':modalInstance,
'ReleaseService':releaseService, 'TrackService':trackService, 'DeliveriesService':deliveriesService});

}));

运行测试时,出现错误:

TypeError: 'undefined' is not an object (evaluating 'releaseService.releases('HIGH').and') at test-release.controller.spec.js:93 TypeError: 'undefined' is not an object (evaluating 'releaseService.releases(vm.currentWorkspace).then') undefined

这是来自 release.service.js

的实际 releaseService
releaseService.releases = getReleases;
.
.
.
function getReleases(workspace){
var releases = [];
headers.Workspace = workspace; // set the workspace
var deferred = $q.defer();
// Then make $http calls and return a promise
.
.

最佳答案

我看到一些可能是问题的事情:

  1. 调用release方法:

releaseService.releases('HIGH').and.returnValue(mockReleasesData);

releases 方法在这里被调用,如果返回值应该在这里设置那么我认为语法可能是:

releaseService.releases.and.returnValue(mockReleasesData);

然后您可以断言 releases 使用正确的数据调用。

  1. 解决上述问题后。 mockReleaseData 没有 then 方法。因为它是从 releases 返回的,所以它的 then 方法将被调用:

releaseService.releases(vm.currentWorkspace).then(函数 (responseValues)

   var mockReleasesData = {
"test" : 100
},
then: function(callbackFn) {
// could call callbackFn with fake responseValues
}
};

测试的目的是什么?是不是要行使then匿名函数?通过少量重组来公开您的逻辑,测试起来可能很简单。

function handleResponseValues(vm, responseValues) {
vm.albums = responseValues;
if(vm.albums.length !== 0){
vm.selected.album = vm.albums[0];
}

这仍然需要 then 回调调用 handleResponseValues 并引用 vmresponseValues 但是将公开所有独立于 promise 链的逻辑。

关于javascript - AngularJS 类型错误 : 'undefined' is not an object' Jasmine test,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37779965/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com