简单角度茉莉花测试不能按预期工作

时间:2014-07-14 19:20:27

标签: angularjs jasmine

下面是一个简单的Jasmine测试。出于某种原因,expect(scope.myForm.$invalid).toBe(true)失败了。其他测试正在通过。

我无法弄清楚原因。

谢谢!

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);
 });

1 个答案:

答案 0 :(得分:3)

编译元素后添加scope.$digest();

您可以在此处详细了解:$apply vs $digest in directive testing