Question

ngFormController is created for you when you give a form a name.

<form name="accountForm"> will create $scope.accountForm and this allows you to do things like $scope.accountForm.$setPristine()

However, in my controller tests accountForm is undefined because I suppose it's created after the template is parsed and I'm not sure the template is even considered when testing the controller. How can I mock this up in the test?

Était-ce utile?

La solution

I just had this same issue. What I ended up doing was using sinon.stub to make a fake form object. This object had these properties:

form = {$valid: true, $pristine: false, $dirty: true, $setPristine: function () {scope.newItemForm.$dirty = false, scope.newItemForm.$pristine = true;}};

I then set that fake to equal my scope's newItemForm.

scope.newItemForm = form;

I then tested to see if the behavior was correct, whether scope.newItemForm.$dirty = false and scope.newItemForm.$pristine = true when $setPristine was triggered and the opposite when it wasn't supposed to be triggered.

This doesn't mock all the behaviors of $setPristine, but I figure I just need to know if the method is triggered since I shouldn't be concerned about testing angular's functionality.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top