如何在angularjs测试中注入控制器?

时间:2014-12-08 11:39:10

标签: angularjs jasmine karma-jasmine

我一般是角度和TDD的新手,我一直在尝试调试其他人的代码;代码beolow试图注入一个控制器,但它失败了,我很困惑如何调试这个,任何有关如何解决这个问题的提示将不胜感激

 describe('WebApp: AccountController', function() {
  beforeEach(function() {
    module('webApp');
    module('tpls');
    return inject(function($injector) {
      this.q = $injector.get('$q');
      this.userMock = {
        id: 1,
        email: 'pitogsdfs@gmail.com',
        name: 'Adones Pitogo'
      };
      this.modalMock = {
        open: (function(_this) {
          return function(tpl, ctrl) {
            _this.def = _this.q.defer();
            _this.def.resolve(true);
            return {
              result: _this.def.promise
            };
          };
        })(this)
      };
      this.modalSpy = spyOn(this.modalMock, 'open').andCallThrough();
      this.teamMock = {
        id: 1
      };
      this.repoMock = {
        id: 1
      };
      this.httpBackend = $injector.get('$httpBackend');
      this.httpBackend.whenGET('/api/v1/session').respond({
        status: 'ok',
        user: this.userMock
      });
      this.httpBackend.whenGET("/api/v1/users/" + this.userMock.id + "/teams").respond({
        status: 'ok',
        teams: [this.teamMock]
      });
      this.httpBackend.whenGET("/api/v1/users/" + this.userMock.id + "/repositories/remote").respond({
        status: 'ok',
        repositories: []
      });
      this.httpBackend.whenGET("/api/v1/users/" + this.userMock.id + "/repositories/followed").respond({
        status: 'ok',
        repositories: []
      });
      this.InvoiceService = $injector.get('Common.InvoiceService');
      this.TeamService = $injector.get('Common.TeamService');
      this.RepositoryService = $injector.get('Common.RepositoryService');
      this.UserService = $injector.get('Common.UserService');
      this.rootScope = $injector.get('$rootScope');
      this.scope = this.rootScope.$new();
      this.$controller = $injector.get('$controller');
      this.timeout = $injector.get('$timeout');
      this.compile = $injector.get('$compile');
      this.rootElement = $injector.get('$rootElement');
      this.toasterMock = {
        pop: function(type, title, message) {
          return true;
        }
      };
      this.modalMock = {
        open: (function(_this) {
          return function(opts) {
            var def;
            def = _this.q.defer();
            def.resolve(true);
            return {
              result: def.promise
            };
          };
        })(this)
      };
      this.modalSpy = spyOn(this.modalMock, 'open').andCallThrough();
      this.toasterSpy = spyOn(this.toasterMock, 'pop').andCallThrough();
      this.TeamServiceSpy = spyOn(this.TeamService, 'getUserTeams').andCallThrough();
      $('body').append('<div id="profile-pic-input"></div>');
      this.httpBackend.flush();
      this.controller = this.$controller('AccountController', {
        '$scope': this.scope,
        '$rootScope': this.rootScope,
        'Common.RepositoryService': this.RepositoryService,
        'Common.UserService': this.UserService,
        'Common.InvoiceService': this.InvoiceService,
        'toaster': this.toasterMock,
        'Common.TeamService': this.TeamService,
        '$modal': this.modalMock
      });
      return this.scope.$digest();
    });
  });
  return it('should have an Account Controller', function() {
    expect(this.controller).not.toBe(null);
    return expect(this.controller).not.toBe(void 0);
  });
});

0 个答案:

没有答案
相关问题