gpt4 book ai didi

javascript - 错误: [$resource:badcfg] query when testing a resource service with Jasmine

转载 作者:行者123 更新时间:2023-12-02 15:36:11 25 4
gpt4 key购买 nike

测试我的 REST 服务时,我收到以下错误消息:

Error: [$resource:badcfg] query http://errors.angularjs.org/1.4.6/$resource/badcfg?p0=array&p1=object&p2=GET&p3=http%3A%2F%2Flocalhost%3A63831%2Fapi%2Fnames in http://localhost:60694/js/angular.js (line 68)

我不知道如何解决这个问题。该应用程序是否可能与 WebApi 一样位于另一个本地主机端口中?

下面的代码显示了单元测试:

describe('Test', function () {
var $httpBackend, $rootScope, crudService, baseUrl, respValue;

beforeEach(function () {
module('testApp');

inject(function (_$httpBackend_, _$rootScope_, _CrudService_, _baseUrl_) {
$httpBackend = _$httpBackend_;
$rootScope = _$rootScope_;
crudService = _CrudService_;
baseUrl = _baseUrl_;
});
});

it('should make a request to the backend', function () {
$httpBackend.whenGET('views/home.html').respond(200);
$httpBackend.expectGET(baseUrl + '/api/names').respond(200, [{
Id: '1', fname: 'John', lname: 'Doe', age: 30, GroupId: 1
}]);

$rootScope.$digest();

crudService.getAllNames().$promise.then(function (response) {
respValue = response;
});

$httpBackend.flush();

expect(respValue).toContain([{
Id: '1', fname: 'John', lname: 'Doe', age: 30, GroupId: 1
}]);
});
<小时/>

编辑:

我尝试了下面的答案并添加了 expect() 方法,并显示了以下错误消息:

Expected [ d({ Id: '1', fname: 'John', lname: 'Doe', age: 30, GroupId: 1 }), $promise: Promise({ $$state: Object({ status: 1, pending: undefined, value: , processScheduled: false }) }), $resolved: true ] to contain [ Object({ Id: '1', fname: 'John', lname: 'Doe', age: 30, GroupId: 1 }) ].

最佳答案

很可能是这一行有问题

$httpBackend.expectGET(baseUrl + '/api/names').respond(200, {
Id: '1', fname: 'John', lname: 'Doe', age: 30, GroupId: 1
});

响应应该是一个数组,但它是一个对象。您可以将此行更改为

 $httpBackend.expectGET(baseUrl + '/api/names').respond(200, [{
Id: '1', fname: 'John', lname: 'Doe', age: 30, GroupId: 1
}]);

并尝试。

关于javascript - 错误: [$resource:badcfg] query when testing a resource service with Jasmine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32906603/

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