How do I test an AngularJS route template with Jasmine

时间:2015-10-29 15:56:08

标签: angularjs karma-jasmine

I have Karma/Jasmine setup, and I can test my Controllers. I would like to be able to load my route templates in a test, and bind with a controller to verify that the template is complete and the bindings are setup correctly. For this layer of testing, I don't want to deploy the app yet.

I'm trying to follow some other recommendations and use the ControllerAs syntax in my template.

So I have a template that looks something like this...

<p>Name: <input type="text" ng-model="page.firstName"/></p>
<p>Name: <input type="text" ng-model="page.lastName"/></p>
Full Name :<p ng-bind="page.fullName()"></p>

There is a route setup for this. However, I would prefer to skip the routing in the test, and be able to load the template and apply my context manually.

$routeProvider.when("/", 
              {
                    templateUrl: "../resources/html/sampleEdit.html", 
                    controller: "SampleEditController", 
                    controllerAs: 'page'     
              })

If I try to use $compile(template)($scope), the ng-model elements are ignored, because this is not a directive, this is a template

I've also tried using jasmine-jquery fixtures. Trying every combination of ng-app, ng-controller I can think of. But if I can't get even this example to work.

 var s  = jasmine.getFixtures().set("{{2+3}}");
 expect(s).toMatch("5");

Any ideas, pointers? I'm pretty new to Angular, so I'm probably missing something basic.

0 个答案:

没有答案