How to clean cache before $httpBackend.flush() in

2019-08-13 09:49发布

I need to make unit test for a directive in my Angular project. This directive refer a service which use $http required get data and then store them in local cache. and my test case like this:

    describe('nationality testing',function(){

    it('should get countries',function(){
        var mockCountries=[{"CountryID":1,"Name":"Afghanistan","Nationality":"Afghan"},{"CountryID":2,"Name":"South Africa","Nationality":"South African"},{"CountryID":3,"Name":"Albania","Nationality":"Albanian"},{"CountryID":4,"Name":"Algeria","Nationality":"Algerian"}];
        var expectUrl=casinolink.config.MockServicePrefix+casinolink.config.MockServices.Country;    
        httpBackend.expectGET(expectUrl).respond(201,mockCountries);
        var t=compile("<cl-nationality  clNationality-selected=\"yourNationalityModel\" ><option></option></cl-nationality>")(scope);
        scope.$digest();
        httpBackend.flush(1);
        expect(scope.$$childHead.options.length).toBe(4);
    });
});

when i first time start my karma unit test, all ut passed, but then i refresh my test page again, this test case got failed and an error popped out:

Error: No pending request to flush !
at Error (<anonymous>)
at Function.$httpBackend.flush (http://localhost:9876/absoluteC:/WebUI/WebUI/test/lib/angular/angular-mocks.js:1195:34)
at null.<anonymous> (http://localhost:9876/absoluteC:/WebUI/WebUI/test/unit/directives.spec.js:163:25)
at jasmine.Block.execute (http://localhost:9876/absoluteC:/Users/zhangji/AppData/Roaming/npm/node_modules/karma-jasmine/lib/jasmine.js:1145:17)
at jasmine.Queue.next_ (http://localhost:9876/absoluteC:/Users/zhangji/AppData/Roaming/npm/node_modules/karma-jasmine/lib/jasmine.js:2177:31)
at jasmine.Queue.start (http://localhost:9876/absoluteC:/Users/zhangji/AppData/Roaming/npm/node_modules/karma-jasmine/lib/jasmine.js:2130:8)
at jasmine.Spec.execute (http://localhost:9876/absoluteC:/Users/zhangji/AppData/Roaming/npm/node_modules/karma-jasmine/lib/jasmine.js:2458:14)
at jasmine.Queue.next_ (http://localhost:9876/absoluteC:/Users/zhangji/AppData/Roaming/npm/node_modules/karma-jasmine/lib/jasmine.js:2177:31)
at jasmine.Queue.start (http://localhost:9876/absoluteC:/Users/zhangji/AppData/Roaming/npm/node_modules/karma-jasmine/lib/jasmine.js:2130:8)
at jasmine.Suite.execute (http://localhost:9876/absoluteC:/Users/zhangji/AppData/Roaming/npm/node_modules/karma-jasmine/lib/jasmine.js:2604:14) 

I thought this because the service i used store the data into local cache after first access, and then it won't make new request anymore but get all from cache, so when i call $httpBackend.flush() above error will show up. (i'm a newbie in angularJs, this is just my analyze about this error)

my question is how to clean local cache before $httpBackend.flush() ? Any one have different ideas to solve my problems?

update: I open my chrome console and found that angular service store the data in browser Local Storage, Once i delete this storage, my unit test passed, but refresh, it comes again. enter image description here I can't Block sites from setting any data because i need allow store data in first access. How could karma or angular clear this?

1条回答
Evening l夕情丶
2楼-- · 2019-08-13 09:54

I finally find the solution, it's so easy!

clear storage before each spec:

    beforeEach(function(){
    localStorage.clear();
});
查看更多
登录 后发表回答