1

Below is a simple Jasmine test. For some reason expect(scope.myForm.$invalid).toBe(true) is failing. The other tests are passing.

I can't figure out why.

Thanks!

describe('angularValidator', function() {

 beforeEach(module('angularValidator'));

 var scope, compile;

 beforeEach(inject(function($rootScope, $compile) {
   scope = $rootScope.$new();
   compile = $compile;

   var htmlForm = "<form name=\"myForm\">"
                    + "<input  type = \"text\""
                    + "name = \"firstName\""
                    + "ng-model = \"firstName\""
                    + "required></form>";

   element = compile(htmlForm)(scope);

 }));


 it('Initial should be pristine and invalid', function () {
    expect(scope.myForm.$pristine).toBe(true);
    expect(element.hasClass('ng-pristine')).toBe(true);
   expect(scope.myForm.$invalid).toBe(true);
 });
Jakobovski
  • 3,203
  • 1
  • 31
  • 38

1 Answers1

3

Add a scope.$digest(); after compiling your element.

You can read more aout it here: $apply vs $digest in directive testing

Community
  • 1
  • 1
Daniel
  • 6,916
  • 2
  • 36
  • 47