yuro
yuro

Reputation: 2209

ERR: [$resource:badcfg] query when testing a resource service with Jasmine

When testing my REST service I'm getting the following error message:

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)

I don't know how to solve the problem. Is it possible that the app is in another localhost port as the WebApi?

The code below is showing the unit-test:

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
            }]);
        });

EDIT:

I tried the answer below and added an expect()-method and the following error message was displayed:

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 }) ].

Upvotes: 0

Views: 257

Answers (1)

Chandermani
Chandermani

Reputation: 42669

Most probably this line is the problem

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

The response should have been an array but it is a object. Can you change this line to

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

and try.

Upvotes: 2

Related Questions