jeudi 30 juin 2016

Testing angular js forms

I had a simple form, something like this

<form class="Form" name="form"> 
  <input type="text">
</form>

But then I had to use two different forms and show them based on some condition. To achieve that, I used ng-if

<form class="Form" name="forms.someForm" ng-if="someCondition()"> 
  <input type="text">
</form>

<form class="Form" name="forms.someForm" ng-if="!someCondition()"> 
  <input type="text">
</form>

and I created an object on controller

$scope.forms = {}

This seems to be working fine now. But this change broke my unit tests. Right now, in unit test spec I have form reference as

scope.form = {}

But when I run Jasmine test, it gives me this error

TypeError: 'undefined' is not an object (evaluating $scope.forms.someForm.$invalid)

Where

$scope.forms.someForm.$invalid

happens to be a part of controller's code.

What change I need to make to the test spec? Can anyone please help?

Aucun commentaire:

Enregistrer un commentaire