与噶AngualrJS - 我怎样写一个工厂单元测试?(AngualrJS with Karma

2019-09-28 18:38发布

我写一个简单的AngularJS 1.x的Web应用程序。

我有一个模块:

main.js:

var app = angular.module('app', []);

factory.js

app.factory('DataFactory', function(){

  var DataService = {};

  DataService.something = function() {
    return 5;
  };

  return DataService;

});

controller.js

app.controller('DataController', function ($scope, DataFactory) {
    $scope.searchText = null;
    $scope.results = DataFactory.something();
});

index.html的:

<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="utf-8">
        <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
</script>
    </head>
    <body ng-app="app" ng-controller="DataController">
        <script src="app.js"></script>
        <script src="factory.js"></script>
        <script src="controller.js"></script>
    </body>
</html>

测试文件:

  describe('Data Factory Test', function() {

    var Factory;

    beforeEach(function() {
      angular.module('app');
    });

    beforeEach(inject(function() {
      var $injector = angular.injector(['app']);
      Factory = $injector.get('DataFactory');
    }));

    it('is very true', function(){
        expect(Factory).toBeDefined();
      // var output = Factory.something();
      // expect(output).toEqual(5);
    });

  });

karma.conf.js:

frameworks: ['jasmine'],


// list of files / patterns to load in the browser
files: [
    'node_modules/angular/angular.js',
    'node_modules/angular-mocks/angular-mocks.js',
    'app.js',
    'factory.js',
    'controller.js',
    'test/*.js'
]

我怎样写一个单元测试,以检查是否于工厂存在,并检查的东西回报?

我不断收到一个错误,当我运行因果报应的开始:错误:[$注射器:modulerr]未能实例化模块的应用程序,因为:错误:[$注射器:unpr]未知提供商:$ controllerProvider

编辑:我得到它的工作。 我怎么会写有和没有一个工厂控制器单元测试?

Answer 1:

第一部分显示服务/工厂如何进行测试。 第二部分显示控制器测试的两种方式

  • 我们只是希望,从$范围的某些变量已经改变
  • 我们期待着某些服务/工厂被称为

也许所有这些类型的测试涵盖了我们所有的需求。

 angular.module('app', []).factory('DataFactory', function() { var DataService = {}; DataService.something = function() { return 5; }; return DataService; }).controller('DataController', function($scope, DataFactory) { $scope.searchText = null; $scope.results = DataFactory.something(); }); describe('Data Factory Test', function() { var Factory; beforeEach(module('app')); beforeEach(inject(function(_DataFactory_) { Factory = _DataFactory_ })); it('is very true', function() { expect(Factory).toBeDefined(); var output = Factory.something(); expect(output).toEqual(5); }); }); describe('DataController ', function() { var $scope, instantiateController, DataFactory beforeEach(module('app')); beforeEach(inject(function($rootScope, $controller, _DataFactory_) { $scope = $rootScope.$new() DataFactory = _DataFactory_ instantiateController = function() { $controller('DataController', { $scope: $scope, DataFactory: DataFactory }) } })) // It shows that controller chenges $scope.results it('Calculates results', function() { expect($scope.results).toBe(undefined) instantiateController() expect($scope.results).toBe(5) }) // It shows that DataFactory was called it('Calls `DataFactory.something`', function() { spyOn(DataFactory, 'something'); instantiateController() expect(DataFactory.something).toHaveBeenCalled() }) }); 
 <link href="//safjanowski.github.io/jasmine-jsfiddle-pack/pack/jasmine.css" rel="stylesheet" /> <script src="//safjanowski.github.io/jasmine-jsfiddle-pack/pack/jasmine-2.0.3-concated.js"></script> <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular.min.js"></script> <script src="//ajax.googleapis.com/ajax/libs/angularjs/1.5.0/angular-mocks.js"></script> 



Answer 2:

你需要调用angular.injector

'use strict';

(function() {
  describe('Factory Spec', function() {

    var Factory;

    beforeEach(function() {
      angular.module('app');
    });

    beforeEach(inject(function() {
      var $injector = angular.injector(['app']);
      Factory = $injector.get('DataFactory');
    }));

    it('is very true', function(){
      var output = Factory.something();
      expect(output).toEqual(5);
    });

  });
  }());

要测试的控制器:

describe('PasswordController', function() {
  beforeEach(module('app'));

  var $controller;

  beforeEach(inject(function(_$controller_){
    // The injector unwraps the underscores (_) from around the parameter names when matching
    $controller = _$controller_;
  }));

  describe('$scope.grade', function() {
    it('sets the strength to "strong" if the password length is >8 chars', function() {
      var $scope = {};
      var controller = $controller('PasswordController', { $scope: $scope });
      $scope.password = 'longerthaneightchars';
      $scope.grade();
      expect($scope.strength).toEqual('strong');
    });
  });
});

从这里 ;



文章来源: AngualrJS with Karma - how do I write a unit test for a factory?