如何清洗之前$ httpBackend.flush()缓存AngularJs单元测试? [英] How to clean cache before $httpBackend.flush() in AngularJs Unit Test?

查看:167
本文介绍了如何清洗之前$ httpBackend.flush()缓存AngularJs单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要做单元测试在我的角度项目中的指令。这个指令是指其使用需要$ HTTP GET数据,然后将它们存储在本地缓存的服务。我的测试案例是这样的:

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

当我第一次开始我的人缘单元测试,全部通过了UT,但后来我再次刷新我的测试页面,在此测试案例得到了失败和错误蹦出来:

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) 

我想这是因为我的服务后首次访问用于存储数据到本地缓存中,然后它不会让新的请求了,但是从缓存中获得所有,所以当我叫 $ httpBackend。冲洗()上述错误就会显示出来。 (我在angularJs一个新手,这只是我的分析有关这个错误)

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)

我的问题是如何前$ httpBackend.flush清理本地缓存()?
任何一个有不同的想法来解决自己的问题?

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. 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?

推荐答案

我终于找到解决方案,它是那么容易!

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

每个天赋之前清除存储:

clear storage before each spec:

    beforeEach(function(){
    localStorage.clear();
});

这篇关于如何清洗之前$ httpBackend.flush()缓存AngularJs单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆