spyOn could not find an object to spy upon for all()

23,397

This is how I did it, hope it helps.

var Restangular;
beforeEach(inject(function( _Restangular_) {
    Restangular = _Restangular_;
    spyOn(Restangular, 'all').and.callThrough();
}));
Share:
23,397
forgottofly
Author by

forgottofly

Passionate front end developer eager to explore the web SOreadytohelp

Updated on July 31, 2020

Comments

  • forgottofly
    forgottofly almost 4 years

    I'm getting the above error when I want to mock http REST services created using Restangular. Here's my piece of code:

    var someParameter = [{ "id": 1, "name": "Length" }]
       spyOn(Restangular, 'one').and.callThrough();
        it('expect company service to be called', function () {     
            httpBackend.expectGET('http://localhost:8283/com/companies', {
                someParameter: someParameter
            }).respond(mockToReturn);
        });
    

    Has anybody encountered the same issue.And any fix ?

  • androidtitan
    androidtitan over 7 years
    Trying to use inject and it is near impossible to debug because there is no explanation as to what it is or why to use it.
  • danwellman
    danwellman over 7 years
    No, spies should be added in a beforeEach
  • crackmigg
    crackmigg about 6 years
    For reference: it is angular.mock.inject.