Unit Testing and $dirty

1,652 views
Skip to first unread message

Michael Bielski

unread,
Mar 12, 2013, 5:04:44 PM3/12/13
to ang...@googlegroups.com
Some of the forms I have in my project need to prompt the user if they try to leave after changing something and not saving it. To facilitate this, I am checking $scope.formName.$dirty and displaying prompts based upon that. Everything was going dandy until I started implementing unit testing. My unit tests for the controller of that form now fail because $scope.formName is undefined, and therefore can't have a $dirty property.

Is there a right way to mock the presence of a form? $compile perhaps?

If I shouldn't be checking things that way, how should I be doing it? a directive?

Peter Bacon Darwin

unread,
Mar 12, 2013, 5:06:53 PM3/12/13
to ang...@googlegroups.com
If you are purely testing the controller, then just create a mock object:

beforeEach(function() {
  scope.formName = {
    $dirty: false
  };
});


--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to angular+u...@googlegroups.com.
To post to this group, send email to ang...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en-US.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Michael Bielski

unread,
Mar 12, 2013, 5:13:34 PM3/12/13
to ang...@googlegroups.com
Yes, I thought of that, but then later on I am using $setPristine(), which means that I have to mock that method. Is that still "kosher"?

Peter Bacon Darwin

unread,
Mar 12, 2013, 5:18:50 PM3/12/13
to ang...@googlegroups.com
spyOn(scope.formName, '$setPristine')

then you can do (later on)...

expect(scope.formName.$setPristine).toHaveBeenCalled();

Feels good to me!


On 12 March 2013 21:13, Michael Bielski <michael...@yahoo.com> wrote:
Yes, I thought of that, but then later on I am using $setPristine(), which means that I have to mock that method. Is that still "kosher"?

--

Michael Bielski

unread,
Mar 12, 2013, 5:38:22 PM3/12/13
to ang...@googlegroups.com
Works for me! Thanks Peter!
Reply all
Reply to author
Forward
0 new messages