gpt4 book ai didi

javascript - Jasmine spy : Getting an error in returnValue

转载 作者:行者123 更新时间:2023-11-30 14:51:29 26 4
gpt4 key购买 nike

我正在尝试用 jasmine spy 编写单元测试用例。下面是我的服务代码。

function reset(someParam) {
var deferred = $q.defer();

svcTypes.getTasksWithRoles().then(function (types) {
if (types.HadError) return;

// do some stuff..

deferred.resolve(results);
}, function (errorResponse) {
deferred.reject(errorResponse);
});
return deferred.promise; }

下面是我的单元测试用例:

describe('tests for svcWorkordertypes', function () {

beforeEach(angular.mock.module('workorders.service'));
beforeEach(angular.mock.module('workordertypes.service'));


var svcWorkordertypes;
var svcTypes;
var $q, $httpBackend;
beforeEach(angular.mock.inject(function (_svcWorkordertypes_, _svcTypes_, _$q_, _$httpBackend_) {
svcWorkordertypes = _svcWorkordertypes_;
svcTypes = _svcTypes_;
$q = _$q_;
$httpBackend = _$httpBackend_;

}));

//This is working fine.
it('all controls should be set', function () {
expect(svcWorkordertypes).toBeDefined();
expect(svcTypes).toBeDefined();
expect($q).toBeDefined();
expect($httpBackend).toBeDefined();

});

it('test reset method', function () {
//Arrange
var someParam = 'test';
var types = [{ RoleList: 'newRoleList', isHeader: true, Roles: 'testRole' }];
$httpBackend.when("GET", 'null/tasks/types?$select=IncludeRoles').respond(200, JSON.stringify(types));
spyOn(svcTypes, 'getTasksWithRoles').and.returnValue(function () {
var deferred = $q.defer();
deferred.resolve(JSON.stringify(types));
return deferred.promise;
});

//Act
var result = svcWorkordertypes.reset(selectedRoleName);


//Assert
result.then(function (res) {
console.log(res + ' res output...');
//expect(JSON.stringify(res)).toEqual(JSON.stringify(types));
});
//expect(svcTypes).toBeDefined();
}) });

错误如下:

enter image description here

我也用 callFake 尝试了很多方法。我在 stackoverflow 上发现了类似的问题但这对我没有帮助。

最佳答案

getTasksWithRoles 应该返回一个 promise ,它被模拟为返回一个返回 promise 的函数。

应该是:

spyOn(svcTypes, 'getTasksWithRoles').and.returnValue(
$q.resolve(JSON.stringify(types))
);

关于javascript - Jasmine spy : Getting an error in returnValue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48041507/

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