describe("File Upload directive", function() {
var elm, scope,httpBackend , controller , isolateScope;
beforeEach(angular.mock.module("fileApp"));
beforeEach(angular.mock.inject(function($rootScope, $compile , $httpBackend) {
elm = angular.element("<div file-upload base-upload-url='files/' ></div>");
httpBackend = $httpBackend;
scope = $rootScope;
httpBackend.whenGET("fileUploadTemplate.html").respond(true);
$compile(elm)(scope);
controller = elm.controller;
scope.$digest();
}));
it("should upload selected file",function(){
httpBackend.flush();
isolateScope = elm.isolateScope();
expect(isolateScope).toBeDefined();
isolateScope.selectedFiles=[{"webkitRelativePath":"","lastModifiedDate":"2014-05-26T11:15:55.000Z","name":"a.csv","type":"text/csv","size":131}];
var index = 0;
httpBackend.expectPOST("files/").respond({"fileId":67603});
expect(isolateScope.uploadResult.length).toBe(1);
//httpBackend.flush();
});
});
Now, here I have situations:
- If I comment out
httpBackend.flush()
at the start ofit
block , isolateScope isundefined
. - If I comment out
httpBackend.flush()
at the end ofit
block , theisolateScope.uploadResult.length = 0
which suggestshttpPOST
didn't execute correctly as expected (httpBackend.flush is never called after the http call).
3.If I keep both httpBackend.flush()
(which is not supposed to be done) , I get error as expected : Error: No pending request to flush !
.
Can anyone please guide me ? I am a newbie to karma.
Thanks in advance.