Angular resource testing: $httpBackend.flush() cause Unexpected request

后端 未结 2 652
甜味超标
甜味超标 2021-02-06 05:25

I want to test angularjs resource.

\'use strict\';

/**
 * AddressService provides functionality to use address resource in easy way.
 *
 * This is an example us         


        
2条回答
  •  我在风中等你
    2021-02-06 05:58

    the error is caused by your test attempting to load the main page for your app. Since you have not told the test to expect this server call, an error is returned. See the documentation for $httpBackend for clarification on this point.

    Your $templateCache workaround is designed for unit testing directives not anything else. You were quite close with:

    $httpBackend.when('GET', /\.html$/).passThrough();
    

    Since you don't need to do anything with the actual template file it's a safe and simple work around to add this to your beforeEach() block.

    $httpBackend.whenGET(/\.html$/).respond('');
    

    This stops you getting the TypeError you had.

    TypeError: Object # has no method 'passThrough'
    
    
    

    I had the same issue in my unit tests after upgrading to a new version of Angular JS and using respond('') worked fine.

    提交回复
    热议问题