javascript - How to test AngularJS $asyncValidators including a $http call -


"postprint" itemprop = "text">

I'm trying to write a jasmine test for the AngularJS email availability checker, which uses angular's latest feature - $ Async validator pipeline.

  au_helper.directive ('recordAvailabilityValidator', ['$ http', function ($ http) {return required: 'ngModel', link: function (scope, element here my instructions, attrs, ngModel) {var apiUrl = attrs.recordAvailabilityValidator; var ownId = attrs.recordAvailabilityId; ngModel $ asyncValidators.unavailable = function (value) {return $ http ({method :. 'get by' URL: apiUrl, parameter : {V: value, id: ownId}})}}}}}}]);  

In my trial, try to setback backend response to me like:

  $ httpBackend.when ('Received', 'api / users / emailAvailable V =test@email.com '). Analog (400, {Flash: 'not available'}); $ Scope.user = {email: zero}; Var element = angular.element ( '& lt; form name =' form '& gt;' + '& lt; input ng- model = "user.email" name = "email" Record-availability-recognizer = "API / User / email available "/ & gt; '+' & lt; / form & gt; '); $ Compilation (element) ($ scope); Form = $ scope.form;  

And then test it:

  this ('should show an error when creating records with existing email', function () {form.email . $ setViewValue ('test@email.com '); $ scope $ Digest () ;. expect .toBeDefined () (form.email $ error.unavailable.);});  

However, $ error.unavailable is always undefined.

Thank you for your help!

You really need to flush $ httpBackend for calls in

< pre> it should show an error when creating a record with ( 'existing email', function () {form.email. $ SetViewValue ('test@email.com '); $ scope. $ Digest (); $ HttpBackend.flush (); Hopefully (form.email.error.unavailable) .toBeTruthy ();});

Comments