2015-08-19 35 views
0

最後我得到了角度控制器的一個業力測試。但如果我嘗試用另一個控制器執行幾乎同樣的測試,它無法與錯誤信息的工作:Error: [ng:areq] Argument 'Test2Controller' is not a function, got undefined一個角度控制器Karma-Jasmine測試工作,但不是另一個

工作測試:

describe('TestController: - ', function() { 
beforeEach(module('myApp')); 
var scope, $controller, httpBackend; 

beforeEach(inject(function ($rootScope, _$controller_, $httpBackend) { 
    scope = $rootScope.$new(); 
    httpBackend = $httpBackend; 
    $controller = _$controller_; 
})); 
afterEach(function() { 
    httpBackend.verifyNoOutstandingExpectation(); 
    httpBackend.verifyNoOutstandingRequest(); 
}); 
describe('version testing; -', function() { 
    it("tests the version ", function() { 

     httpBackend.whenGET('url').respond(200, {"meta":{"apiVersion":"0.1","code":200,"errors":null}}); 
     var $scope = {}; 
     var controller = $controller('TestController', { $scope: $scope }); 
     httpBackend.flush(); 
     expect($scope.version.meta.apiVersion).toEqual('0.1'); 
     expect($scope.version1).toEqual('1'); 
    }) 
}) 
}); 

萬物工作在這裏很好。但是這個沒有:

describe('Test2Controller: - ', function() { 
beforeEach(module('myApp')); 
var scope, $controller, httpBackend; 

beforeEach(inject(function ($rootScope, _$controller_, $httpBackend) { 
    scope = $rootScope.$new(); 
    httpBackend = $httpBackend; 
    $controller = _$controller_; 
})); 
afterEach(function() { 
    httpBackend.verifyNoOutstandingExpectation(); 
    httpBackend.verifyNoOutstandingRequest(); 
}); 
describe('test 2 testing; -', function() { 
    it("tests the test2 ", function() { 

     httpBackend.whenGET('url').respond(200, {"meta":{"apiVersion":"0.1","code":200,"errors":null}}); 
     var $scope = {}; 
     var controller = $controller('Test2Controller', { $scope: $scope }); 
     httpBackend.flush(); 
     expect($scope.testVal).toEqual('Test Value'); 
    }) 
}) 
}); 

我也在karma配置中註冊了測試文件,但它只是爲第一個配置工作。沒有測試環境(我的意思是我的純角度應用程序),每個控制器都工作正常。那麼我做錯了什麼?

回答

0

我想知道如果錯誤信息是說實話:至少在這種情況下,沒有'Test2Controller'。也許它被命名爲別的東西?或者它可能存在於你沒有包含在karma.conf.js中的源文件中?不能確切地說,但我猜測你的測試是好的,問題是業力無法找到你的控制器。

您可能還想嘗試瀏覽器實例karma啓動中的調試按鈕。這將打開另一個頁面,您可以在Chrome開發工具或螢火蟲或您最喜愛的JavaScript調試器中檢查。繼續深入瞭解實際加載的業務,並且您可能發現問題。希望這可以幫助。

相關問題