2015-12-15 133 views
3

我越來越 - Error: [$injector:unpr] Unknown provider: $scopeProvider <- $scope運行以下測試時:

describe('mainCtrl', function() { 

beforeEach(module('app')); 
var controller, scope; 
var window = { 
    open: function (url, target, specs) { 
     var spec, specKey; 
     this.href = url; 
     this.target = target; 
     // Parse through the spec string to grab the parameters you passed through 
     var specArray = specs.split(','); 
     for (specKey in specArray) { 
      spec = specArray[specKey].split('='); 
      this[String.trim(spec[0])] = String.trim(spec[1]); 
     } 
    } 
}; 

beforeEach(inject(function ($controller, $window, $rootScope) { 
    scope = $rootScope.$new(); 
    controller = $controller('mainCtrl', {$scope: scope}); 
    window = $window; 
})); 

describe('$scope.popup1', function() { 

    it('should open a popup window when ISIN hyperlink is clicked within grid, passing ISIN object s values to shareDataService', inject(function ($window, $scope) { 
     spyOn($window, 'open').and.callFake(function() { 
      return true; 
     }); 
     scope.popup1() 
     expect(window.href).toEqual("views/Box_Ladder.html"); 
     expect(window.target).toEqual("_blank"); 
     expect(window.height).toEqual(400); 
     expect(window.width).toEqual(700); 
    }) 
    ) 
}) 
}); 

但我不知道爲什麼。我已經注入了範圍(據我所知),並在我的karma.conf.js文件中包含了angular-mocks。

回答

4

這是因爲你試圖注入$scopeit功能:

it('should open a popup window ...', inject(function ($window, $scope) 

只是將其刪除,它應該工作:

it('should open a popup window ...', inject(function ($window) 

就像錯誤狀態,沒有$scopeProvider。測試時,您必須手動創建示波器並對其進行分配,就像您在創建控制器時所做的一樣:

scope = $rootScope.$new(); 
controller = $controller('mainCtrl', {$scope: scope}); 
+0

啊,現在工作謝謝! – xeon48

+0

不客氣:) – tasseKATT

相關問題