Angularjs karma jasmine undefined不是一个对象

时间:2016-05-13 06:17:50

标签: angularjs jasmine karma-runner

我试图在angularjs项目中开始使用单元测试。但是得到这个错误并且无法找出问题原因。

fibonacci.service.js



dictvectorizer




fibonacci.service.tests.js



(function () {
  'use strict';

  angular.module('myFirstApp').service('FibonacciService', FibonacciService);

  //FibonacciService.$inject = [];
  function FibonacciService() {

    // iterative approach
    this.fibonacci = function (num) {
      var prev1 = 1,
        prev2 = 0,
        current = 0;
      for (var n = 2; n <= num; n++) {
        current = prev1 + prev2;
        prev2 = prev1;
        prev1 = current;
      }
      return current;
    };
    
  }
})();
&#13;
&#13;
&#13;

Gruntfile.js

&#13;
&#13;
'use strict';
describe('myFirstApp.FibonacciService', function() {

  var FibonacciService;

  beforeEach(module('myFirstApp'));
  beforeEach(module('app.admin'));
  beforeEach(inject(function($injector) {
    FibonacciService = $injector.get('FibonacciService');
  }));

  it('Should output correct Fibanacci numbers', function() {
    expect(FibonacciService.fibonacci(0)).toBe(0);
    expect(FibonacciService.fibonacci(1)).toBe(1);
    expect(FibonacciService.fibonacci(10)).toBe(55);
  });

});
&#13;
&#13;
&#13;

...
karma: {
      unit: {
        options: {
          frameworks: ['jasmine'],
          singleRun: true,
          browsers: ['PhantomJS'],
          files: [
            'bower_components/angular/angular.js',
            'bower_components/angular-mocks/angular-mocks.js',
            //'app/scripts/**/*.js'
            //'app/scripts/app.js',
            //'app/scripts/services/fibonacci.service.js',

            'app/scripts/core/core.module.js',
            'app/scripts/core/router.module.js',
            'app/scripts/controllers/admin/admin.module.js',
            'app/scripts/app.js',
            'app/scripts/**/*.js'
            //'app/scripts/services/fibonacci.service.tests.js'

          ]
        }
      }
    }
...

因警告而中止。

执行时间(2016-05-13 06:13:27 UTC) 业力:单位2.2s█████████████████████████████████████████████ █████100% 总计2.2秒

请帮助别人。

2 个答案:

答案 0 :(得分:1)

我修复了你的代码,以便成功运行单元测试,如下所示:

  • 服务
(function () {
  'use strict';    
  angular.module('myFirstApp', []).service('FibonacciService', FibonacciService);

  function FibonacciService() {

    // iterative approach
    this.fibonacci = function (num) {

      if (num === 0) {
        return 0;
      }

      if (num === 1) {
        return 1;
      }

      var prev1 = 1,
        prev2 = 0,
        current = 0;

      for (var n = 2; n <= num; n++) {
        current = prev1 + prev2;
        prev2 = prev1;
        prev1 = current;
      }

      return current;
    };
  }
})();
  • 单元测试
'use strict';
describe('myFirstApp.FibonacciService', function () {

  var FibonacciService;

  beforeEach(module('myFirstApp'));
  beforeEach(inject(function ($injector) {
    FibonacciService = $injector.get('FibonacciService');
  }));

  it('Should output correct Fibanacci numbers', function () {
    expect(FibonacciService.fibonacci(0)).toBe(0);
    expect(FibonacciService.fibonacci(1)).toBe(1);
    expect(FibonacciService.fibonacci(10)).toBe(55);
  });
});

答案 1 :(得分:1)

我想出来了。我不得不在Gruntfile.js

中加载所有bower依赖项