【问题标题】:Undefined : value.push(new Resource(item)未定义:value.push(新资源(项目)
【发布时间】:2016-02-18 01:23:03
【问题描述】:

我正在尝试测试工厂,但遇到了一个奇怪的错误。我环顾四周,但未能找到类似的问题。知道我做错了什么吗?

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

});

xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

【问题讨论】:

  • 这可能很愚蠢,但你不应该把expectGET放在flush()之后吗?

标签: javascript angularjs unit-testing karma-jasmine


【解决方案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);

【讨论】:

  • 不。当我更改订单时,我得到Error: Unexpected request: GET api/businesses/abcd
  • 好吧.. 我告诉过你,这可能是愚蠢的 :P 抱歉我帮不上忙
  • 感谢他的努力。将更新角度并查看是否可以解决问题。
猜你喜欢
  • 1970-01-01
  • 2012-04-12
  • 1970-01-01
  • 2016-07-19
  • 2019-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多