AngularJS笔记1

来源:互联网 发布:如何查看网站seo 编辑:程序博客网 时间:2024/05/16 13:53

Do not use controllers to:

  • Manipulate DOM — Controllers should contain only business logic. Putting any presentation logic into Controllers significantly affects its testability. Angular has databinding for most cases and directives to encapsulate manual DOM manipulation.
  • Format input — Use angular form controls instead.
  • Filter output — Use angular filters instead.
  • Share code or state across controllers — Use angular services instead.
  • Manage the life-cycle of other components (for example, to create service instances).

Let's write some tests to show how to override configuration in tests.

describe('myApp', function() {  // load application module (`greetMod`) then load a special  // test module which overrides `$window` with a mock version,  // so that calling `window.alert()` will not block the test  // runner with a real alert box.  beforeEach(module('greetMod', function($provide) {    $provide.value('$window', {      alert: jasmine.createSpy('alert')    });  }));  // inject() will create the injector and inject the `greet` and  // `$window` into the tests.  it('should alert on $window', inject(function(greet, $window) {    greet('World');    expect($window.alert).toHaveBeenCalledWith('Hello World!');  }));  // this is another way of overriding configuration in the  // tests using inline `module` and `inject` methods.  it('should alert using the alert service', function() {    var alertSpy = jasmine.createSpy('alert');    module(function($provide) {      $provide.value('alert', alertSpy);    });    inject(function(greet) {      greet('World');      expect(alertSpy).toHaveBeenCalledWith('Hello World!');    });  });});

0 0
原创粉丝点击