Angular Js:简单的单元测试不起作用

Angular Js: Simple unit test not working

本文关键字:单元测试 不起作用 简单 Js Angular      更新时间:2023-09-26

情况:

正在尝试为我的角度应用程序设置单元测试。我已经创建了一个基本的测试角度应用程序并编写了一个简单的单元测试,但没有按预期工作。

该应用程序:

var app = angular.module( 'myApp', [] );
app.controller('InitCtrl', function( $scope, Person ) 
{
    $scope.test_person = Person.Person("Tom");
})
app.factory('Person', function(){
    return {
        Person: function(name){
            this.name = name;
            console.log ( name );
        }
    }               
})

单元测试:

describe('Person', function () {
  var Person;
  beforeEach(module('myApp'));
  beforeEach(inject(function (_Person_) {
    Person = _Person_;
  }));
  describe('Constructor', function () {
    it('assigns a name', function () {
      expect(new Person('Ben')).to.have.property('name', 'Ben');
    });
  });
});

错误消息:

当我运行测试时,我收到实际的错误消息:

PhantomJS 1.9.8 (Mac OS X 0.0.0) Person "before each" hook: workFn for "assigns a name" FAILED
Error: [$injector:modulerr] Failed to instantiate module myApp due to:
Error: [$injector:nomod] Module 'myApp' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
http://errors.angularjs.org/1.4.4/$injector/nomod?p0=myApp

尝试:

我尝试过使用不同的 sintax:

var app = angular.module( 'myApp' );

但这并不能解决问题。

问题:

这个简单的测试出了什么问题?
应用程序中缺少某些内容,或者测试中存在错误?

谢谢!

很可能

你没有加载你的应用程序.js在你的业力配置文件中:

module.exports = function(config) {
    config.set({
        basePath: '',    
        files: [
            '../../path/to/app.js',
         //...