My angular directive looks like this.
angular.module('directives').directive(“testDirective”,function($q,testService){
return {
restrict:'A',
scope:{
test:'@'
},
controller: function($scope, $element){
},
templateUrl:'testTemplate.html',
link:function(scope, element, attrs){
scope.loadTests = function(){
var deferred = $q.defer();
testService.get('/test_url').success(function (data, status, headers, config) {
if (status == 200) {
scope.dataHolderId = data.id;
deferred.resolve({});
}
});
return deferred.promise;
};
scope.loadTests();
}
}
});
my mocha test code is structured in this way.
describe('directive should be rendered', function() {
var httpBackend, $compile, $rootScope, $templateCache, html, scope, element;
var q;
var testService;
var innerScope;
var deferred;
beforeEach(module('services'));
beforeEach(module('directives'));
beforeEach(module('htmlTemplates'));
beforeEach(inject(function(_$httpBackend_, _$compile_, _$rootScope_,$q,_testService_) {
httpBackend = _$httpBackend_;
$compile = _$compile_;
q = $q;
$rootScope = _$rootScope_;
deferred = $q.defer();
testService = _testService_;
}));
afterEach(function() {
httpBackend.verifyNoOutstandingExpectation();
httpBackend.verifyNoOutstandingRequest();
});
it("should test function with test service ", function() {
var scope = $rootScope.$new();
deferred.resolve();
var html = '<div test-directive test="Hello"></div>';
var element = angular.element(html);
var expectedData = {id:23423};
httpBackend.when('GET', '/test_url').respond(200, { payload: expectedData});
element = $compile(element)(scope);
scope.$digest();
innerScope = element.isolateScope();
expect(innerScope.dataHolderId).to.equal(23423);
//done();
httpBackend.flush();
});
});
My assertion fails with undefined error. Ive mocked my deferred object and the http call as well. Not sure why the assertion is failing. This fails in async mode as well (using done callback)
Aucun commentaire:
Enregistrer un commentaire