1
我需要對我的Angular項目中的指令進行單元測試。該指令引用一個服務,它使用$ http獲取數據,然後將它們存儲在本地緩存中。和我的測試情況是這樣的:
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過去了,但後來我再次刷新我的測試頁面,本次測試的情況下得到了失敗和錯誤彈出:
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.flush()
上述錯誤就會顯示出來。 (我在angularJs一個新手,這只是我的分析有關這個錯誤)
我的問題是如何前$ httpBackend.flush清理本地緩存()? 任何人有不同的想法來解決我的問題?
更新:我打開我的鉻控制檯,發現角服務店在瀏覽器本地存儲數據,一旦我刪除此存儲,我的單元測試通過,但刷新,它又來了。 我不能阻止設置任何數據,因爲我需要讓在第一次訪問存儲數據的網站。 怎麼可能業力或角度明確這一點?
什麼是 「localStorage的」 這裏,是通用的變量? –