')
- .find('div')
- .append(input)
- .append(span);
-var pc = new PasswordCtrl();
-input.val('abc');
-pc.grade();
-expect(span.text()).toEqual('weak');
-$('body').empty();
-```
-
-In angular the controllers are strictly separated from the DOM manipulation logic and this results in
-a much easier testability story as the following example shows:
-
-```js
-function PasswordCtrl($scope) {
- $scope.password = '';
- $scope.grade = function() {
- var size = $scope.password.length;
- if (size > 8) {
- $scope.strength = 'strong';
- } else if (size > 3) {
- $scope.strength = 'medium';
- } else {
- $scope.strength = 'weak';
- }
- };
-}
-```
-
-and the test is straight forward:
-
-```js
-var $scope = {};
-var pc = $controller('PasswordCtrl', { $scope: $scope });
-$scope.password = 'abc';
-$scope.grade();
-expect($scope.strength).toEqual('weak');
-```
-
-Notice that the test is not only much shorter, it is also easier to follow what is happening. We say
-that such a test tells a story, rather then asserting random bits which don't seem to be related.
-
-## Filters
-{@link ng.$filterProvider Filters} are functions which transform the data into a user readable
-format. They are important because they remove the formatting responsibility from the application
-logic, further simplifying the application logic.
-
-```js
-myModule.filter('length', function() {
- return function(text){
- return (''+(text||'')).length;
- }
-});
-
-var length = $filter('length');
-expect(length(null)).toEqual(0);
-expect(length('abc')).toEqual(3);
-```
-
-## Directives
-Directives in angular are responsible for encapsulating complex functionality within custom HTML tags,
-attributes, classes or comments. Unit tests are very important for directives because the components
-you create with directives may be used throughout your application and in many different contexts.
-
-### Simple HTML Element Directive
-
-Let's start with an angular app with no dependencies.
-
-```js
-var app = angular.module('myApp', []);
-```
-
-Now we can add a directive to our app.
-
-```js
-app.directive('aGreatEye', function () {
- return {
- restrict: 'E',
- replace: true,
- template: '
lidless, wreathed in flame, {{1 + 1}} times
'
- };
-});
-```
-
-This directive is used as a tag `
`. It replaces the entire tag with the
-template `
lidless, wreathed in flame, {{1 + 1}} times
`. Now we are going to write a jasmine unit test to
-verify this functionality. Note that the expression `{{1 + 1}}` times will also be evaluated in the rendered content.
-
-```js
-describe('Unit testing great quotes', function() {
- var $compile;
- var $rootScope;
-
- // Load the myApp module, which contains the directive
- beforeEach(module('myApp'));
-
- // Store references to $rootScope and $compile
- // so they are available to all tests in this describe block
- beforeEach(inject(function(_$compile_, _$rootScope_){
- // The injector unwraps the underscores (_) from around the parameter names when matching
- $compile = _$compile_;
- $rootScope = _$rootScope_;
- }));
-
- it('Replaces the element with the appropriate content', function() {
- // Compile a piece of HTML containing the directive
- var element = $compile("
")($rootScope);
- // fire all the watches, so the scope expression {{1 + 1}} will be evaluated
- $rootScope.$digest();
- // Check that the compiled element contains the templated content
- expect(element.html()).toContain("lidless, wreathed in flame, 2 times");
- });
-});
-```
-
-We inject the $compile service and $rootScope before each jasmine test. The $compile service is used
-to render the aGreatEye directive. After rendering the directive we ensure that the directive has
-replaced the content and "lidless, wreathed in flame, 2 times" is present.
-
-
-## Sample project
-See the [angular-seed](https://github.com/angular/angular-seed) project for an example.
-
--
cgit v1.2.3