如何对 Angular Controller 进行单元测试

 我来答
千锋教育
2016-10-16 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
展开全部
Instant Karma

  Karma 是来Angular团队针对JavaScript开发的一个测试运行框架。它很方便的实现了自动执行测试任务从而替代了繁琐的手工操作(好比回归测试集或是加载目标测试的依赖关系)Karma 和Angular的协作就好比花生酱和果冻.
  只需要在Karma中定义好配置文件启动它,接下来它就会在预期的测试环境下的自动执行测试用例。你可以在配置文件中制定相关的测试环境。angular-seed,是我强烈推荐的可以快速实施的方案。在我近期的项目中Karma 的配置如下:
  
  module.exports = function(config) {
  config.set({
  basePath: '../',
  
  files: [
  'app/lib/angular/angular.js',
  'app/lib/angular/angular-*.js',
  'app/js/**/*.js',
  'test/lib/recaptcha/recaptcha_ajax.js',
  'test/lib/angular/angular-mocks.js',
  'test/unit/**/*.js'
  ],
  
  exclude: [
  'app/lib/angular/angular-loader.js',
  'app/lib/angular/*.min.js',
  'app/lib/angular/angular-scenario.js'
  ],
  
  autoWatch: true,
  
  frameworks: ['jasmine'],
  
  browsers: ['PhantomJS'],
  
  plugins: [
  'karma-junit-reporter',
  'karma-chrome-launcher',
  'karma-firefox-launcher',
  'karma-jasmine',
  'karma-phantomjs-launcher'
  ],
  
  junitReporter: {
  outputFile: 'test_out/unit.xml',
  suite: 'unit'
  }
  
  })
  }
  
  这个跟angular-seed的默认配置类似只不过有以下几点不同:
  需要更改浏览器从Chrome 转到PhantomJS, 这样每次跳转时无需再打开新的浏览器窗口,但在OSX系统会有窗口延迟。所以这个插件还有浏览器设置都做了更改。
  由于我的应用需要引用Google的Recaptcha服务因此添加了依赖的recaptcha_ajax.js小文件。这个小配置就像在Karma的配置文件中添加一行代码那么简单。
  autoWatch确实是个很酷的设置,它会让Karma在有文件更改时自动回归你的测试用例。你可以这样安装Karma:
  
  
  用Jasmine设计测试用例
  当使用Jasmine----一种行为驱动开发模式的JavaScript测试框架为Angular设计单元测试用例时大部分 的资源都已可获取。
  这也就是我接下来要说的话题。
  如果你要对AngularJS controller做单元测试可以利用Angular的依赖注入dependency injection 功能导入测试场景中controller需要的服务版本还能同时检查预期的结果是否正确。例如,我定义了这个controller去高亮需要导航去的那个页签:
  
  app.controller('NavCtrl', function($scope, $location) {
  $scope.isActive = function(route) {
  return route === $location.path();
  };
  })
  
  如果想要测试isActive方法,我会怎么做呢?我将检查$locationservice 变量是否返回了预期值,方法返回的是否预期值。因此在我们的测试说明中我们会定义好局部变量保存测试过程中需要的controlled版本并在需要时注入到对应的controller当中。然后在实际的测试用例中我们会加入断言来验证实际的结果是否正确。整个过程如下:
  
  describe('NavCtrl', function() {
  var $scope, $location, $rootScope, createController;
  
  beforeEach(inject(function($injector) {
  $location = $injector.get('$location');
  $rootScope = $injector.get('$rootScope');
  $scope = $rootScope.$new();
  
  var $controller = $injector.get('$controller');
  
  createController = function() {
  return $controller('NavCtrl', {
  '$scope': $scope
  });
  };
  }));
  
  it('should have a method to check if the path is active', function() {
  var controller = createController();
  $location.path('/about');
  expect($location.path()).toBe('/about');
  expect($scope.isActive('/about')).toBe(true);
  expect($scope.isActive('/contact')).toBe(false);
  });
  });
  使用整个基本的结构,你就能设计各种类型的测试。由于我们的测试场景使用了本地的环境来调用controller,你也可以多加上一些属性接着执行一个方法清除这些属性,然后再验证一下属性到底有没有被清除。
  

  那么要是你在调用$httpservice请求或是发送数据到服务端呢?还好,Angular提供了一种
  $httpBackend的mock方法。这样的话,你就能自定义服务端的响应内容,又或是确保服务端的响应结果能和单元测试中的预期保持一致。
  具体细节如下:
  
  describe('MainCtrl', function() {
  var $scope, $rootScope, $httpBackend, $timeout, createController;
  beforeEach(inject(function($injector) {
  $timeout = $injector.get('$timeout');
  $httpBackend = $injector.get('$httpBackend');
  $rootScope = $injector.get('$rootScope');
  $scope = $rootScope.$new();
  
  
  var $controller = $injector.get('$controller');
  
  createController = function() {
  return $controller('MainCtrl', {
  '$scope': $scope
  });
  };
  }));
  
  afterEach(function() {
  $httpBackend.verifyNoOutstandingExpectation();
  $httpBackend.verifyNoOutstandingRequest();
  });
  
  it('should run the Test to get the link data from the go backend', function() {
  var controller = createController();
  $scope.urlToScrape = 'success.com';
  
  $httpBackend.expect('GET', '/slurp?urlToScrape=http:%2F%2Fsuccess.com')
  .respond({
  "success": true,
  "links": ["www.google.com", "angularjs.org", "amazon.com"]
  });
  
  // have to use $apply to trigger the $digest which will
  // take care of the HTTP request
  $scope.$apply(function() {
  $scope.runTest();
  });
  
  expect($scope.parseOriginalUrlStatus).toEqual('calling');
  
  $httpBackend.flush();
  
  expect($scope.retrievedUrls).toEqual(["www.google.com", "angularjs.org", "amazon.com"]);
  expect($scope.parseOriginalUrlStatus).toEqual('waiting');
  expect($scope.doneScrapingOriginalUrl).toEqual(true);
  });
  });
  正如你所见,beforeEach call其实都很类似,唯一不同的是我们是从injector获取$httpBackend而并非直接获取。即使如此,创建不同的测试时还会有一些明显的不同之处。对初学者来说,会有一个afterEachcall 方法来确保$httpBackend在每次用例执行后不会有明显的异常请求。如果你观察一下测试场景的设置和$httpBackend方法的应用就会会发现有那么几点不是那么直观的。
  
  实际上调用$httpBackend的方法也算是简单明了但还不够——我们还得在传值给$scope.$apply的方法中把调用封装到实际测试中的$scope.runTest方法上。这样在$digest被触发后才能处理HTTP请求。而如你所见直到我们调用$httpBackend.flush()方法后$httpBackend才会被解析,这也就保证了我们能在调用过程中去验证返回的结果是否正确(在上面的示例中,controller的$scope.parseOriginalUrlStatusproperty属性将被传递给调用者,我们也因此能实时监控)
  接下来的几行代码都是在调用过程中检测$scopethat属性的断言。很酷吧?
  提示:在某些单元测试中,用户习惯把没有$的范围标记为变量。这个在Angular文档中并没有强制要求或是过分强调,只是我在使用中为了提高可读性和一致性才使用$scopelike这种方式。
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式