2015-10-13 61 views
1

局勢:角JS:簡單的單元測試工作不

我想設立單元測試我的角度應用程序。我創建了一個基本的測試角度應用程序,並編寫了一個簡單的單元測試,但未按預期工作。

應用程序:

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'); 

但它並沒有解決問題。

問題:

什麼是錯的這個簡單的測試?
應用程序中缺少某些東西,或測試中出現錯誤?

謝謝!

回答

1

最可能的是你沒有加載app.js在karma configuration file

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

謝謝。這確實是錯過了。我仍然得到一個錯誤,但至少測試運行:人構造函數分配一個名稱失敗 \t TypeError:'[對象對象]'不是一個構造函數(評估'新人('本''')) – johnnyfittizio