未定义:value.push(新资源(项目)

时间:2016-02-18 01:23:03

标签: javascript angularjs unit-testing karma-jasmine

我试图测试工厂,但我得到了一个奇怪的错误。我环顾四周,但没有找到类似的问题。关于我做错了什么的任何想法?

TypeError: 'undefined' is not a function (evaluating 'value.push(new Resource(item))')

使用angluarjs v1.3.20

factory.js

'use strict';

//Business service used for communicating with the articles REST endpoints
angular.module('businesses').factory('Business', ['$resource',
    function ($resource) {
        return $resource('api/businesses/:businessId', {
            businessId: '@_id'
        }, {
            update: {
                method: 'PUT'
            }
        });
    }
]);

test.js

describe('My Service', function () {


    // Then we can start by loading the main application module
    beforeEach(module(ApplicationConfiguration.applicationModuleName));

    afterEach(inject(function($httpBackend){
        //These two calls will make sure that at the end of the test, all expected http calls were made
        $httpBackend.verifyNoOutstandingExpectation();
        $httpBackend.verifyNoOutstandingRequest();
    }));

    it('mock http call', inject(function($httpBackend, Business) {
        var resource = new Business({
            _id:'abcd'
        });
        var arraya = [{
            _id:'abcd'
        }, {
            _id:'abcde'
        }];
        //Create an expectation for the correct url, and respond with a mock object
        $httpBackend.expectGET('api/businesses/abcd').respond(200, arraya)

        resource.$query();

        //Because we're mocking an async action, ngMock provides a method for us to explicitly flush the request
        $httpBackend.flush();

        //Now the resource should behave as expected
        console.log(resource);
        //expect(resource.name).toBe('test');
    }));

});

xxxxxxxxxxxxxxxxxxxxxxxxxxx

1 个答案:

答案 0 :(得分:-1)

在你脸红之后,你不应该期待吗?

resource.$query();

//Because we're mocking an async action, ngMock provides a method for us to explicitly flush the request
$httpBackend.flush();

//Create an expectation for the correct url, and respond with a mock object
$httpBackend.expectGET('api/businesses/abcd').respond(200, arraya);