I have been learning AngularJS and things have been going pretty smoothly regarding unit testing, but I\'ve reached a bit of a tricky spot.
Suppose I have a simple f
Alternatively, if you are using WebPack with karma-webpack - you can include the template with require, without the need of karma-ng-html2js-preprocessor package:
describe("MyCtrl's form", function () {
var $scope,
MyCtrl;
beforeEach(angular.mock.module("my.module"));
beforeEach(inject(function (_$rootScope_, _$controller_, _$compile_) {
$scope = _$rootScope_.$new();
// Execute the controller's logic
// Omit the ' as vm' suffix if you are not using controllerAs
MyCtrl = _$controller_("MyCtrl as vm", { $scope: $scope });
// Compile the template against our scope to populate form variables
var html = require("./my.template.html"),
template = angular.element(html);
_$compile_(template)($scope);
}));
it('should be invalid when given bad input', function () {
MyCtrl.form.number.$setViewValue('Not a number');
expect(MyCtrl.form.number.$valid).toBeFalsy();
expect(MyCtrl.form.$valid).toBeFalsy();
});
});
HTML: