2015-09-28 31 views
0

由於某些原因,當我運行測試時,Jasmine告訴我'DataFactory'未定義。任何想法爲什麼?當使用jasmine進行測試時,注入的服務不確定

describe('Practice', function(){ 
beforeEach(module('MyApp')); 
var ctrl; 
beforeEach(inject(function($controller, DataFactory){ 
    spyOn(DataFactory, 'getArtists').andCallThrough(); 
    ctrl = $controller('ArtistCtrl') 
})); 

it('should have array available on load', function(){ 
    expect(DataFactory.getArtists).toHaveBeenCalled(); 
    expect(DataFactory.getArtists.callCount).toEqual(1); 
}); 

});

angular.module('MyApp') 
.factory('DataFactory', ['$http', function($http){ 
    return { 
     getArtists: function(){ 
      return $http.get('artists.json'); 
     } 
    } 
}]); 

回答

0

爲了獲得支持,您必須顯示定義DataFactory的代碼!

在這裏,我看到另外一個錯字:

expect(DataFactory.getArtists).toHaveBeenCalled(); 
expect(dataFactory.getArtists.callCount).toEqual(1); <-- DataFactory instead of dataFactory 
0

您需要在您的測試定義的DataFactory。

這樣做。

describe('Practice', function(){ 
var ctrl; 
var DataFactory; 

beforeEach(module('MyApp')); 
beforeEach(inject(function($controller, _DataFactory_){ 
    DataFactory = _DataFactory_; 
    spyOn(DataFactory, 'getArtists').andCallThrough(); 
    ctrl = $controller('ArtistCtrl', { 
     DataFactory: DataFactory 
    }); 
})); 

it('should have array available on load', function(){ 
    expect(DataFactory.getArtists).toHaveBeenCalled(); 
    expect(DataFactory.getArtists.callCount).toEqual(1); 
}); 
相關問題