[英]$controller is not a function in Angular/Jasmine
我正在通過Angular進行測試。 我試圖實例化一個控制器,而茉莉花抱怨$ controller不是一個函數。 以下是我的測試。
describe "MyCtrl test", () ->
beforeEach module('ngRoute')
beforeEach module('myControllers')
$controller = undefined
$scope = undefined
$routeParams = undefined
mySvc = undefined
beforeEach inject (_$controller_, $rootScope, _$routeParams_,_mySvc_) ->
$controller = _$controller_
$scope = $rootScope.$new()
mySvc = _mySvc_
describe "MyCtrl", () ->
beforeEach () ->
MyCtrl = $controller('MyCtrl', {$scope:$scope,$routeParams:$routeParams,mySvc:mySvc})
it "should be defined", () ->
expect(MyCtrl).toBeDefined()
我在頂級范圍內定義了$ controller,我注入了控制器服務並將其分配給全局變量。 它不是功能嗎?
嘗試這個:
describe "MyCtrl test", () ->
beforeEach module('ngRoute')
beforeEach module('myControllers')
myController = undefined
$scope = undefined
$routeParams = undefined
mySvc = undefined
createCtrl = undefined
beforeEach inject($controller, $rootScope, _$routeParams_,_mySvc_) ->
myController = $controller
$scope = $rootScope.$new()
mySvc = _mySvc_
createrCtrl = ->
myController('MyCtrl', {$scope:$scope,$routeParams:$routeParams,mySvc:mySvc})
it "should be defined", () ->
createCtrl()
expect(MyCtrl).toBeDefined()
在過去的幾天里,我一直在使用SublimeText,而我的公司已獲得WebStorm的許可證。 從頭開始重寫代碼似乎已經解決了該問題。 我嘗試將制表符轉換為空格-解決了我遇到的一個問題,但是它不能解決$ controller錯誤,但是從頭開始可以解決。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.