0

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

yuro
  • 2,189
  • 6
  • 40
  • 76
  • I got a bad cfg error when using $resource, my problem turned out to be the header of my rest call. I had to set it like: headers: { "Accept": "application/json; odata=verbose" } – sch Oct 02 '15 at 11:57
  • @user3266024 Where I have to add this in my unit-test? – yuro Oct 02 '15 at 11:58
  • I have no idea, I'm very new to programming in general, I just thought I'd share my solution to the same error. I'm sorry if I only confused you more :( – sch Oct 02 '15 at 12:07
  • @user3266024 no problem.. I have this error since several days. – yuro Oct 02 '15 at 12:22

1 Answers1

2

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.

Chandermani
  • 42,589
  • 12
  • 85
  • 88
  • Ok now, I'm getting the following message in my testing list: `SPEC HAS NO EXPECTATIONS should make a request to the backend` ..I know I need a `expect()` method. But I don't know how I can test the expected GET request?! – yuro Oct 02 '15 at 12:39
  • When you setup `expectGET` test will fail in there is no GET made. You can test it by commenting the `getAllNames` call. If nothing is failing than expectGET assertion worked. – Chandermani Oct 02 '15 at 12:45
  • I've edited my post. you can see the new error message above – yuro Oct 02 '15 at 12:55