2014-10-10 95 views
0

我試圖做一些非常類似於egghead.io上的示例。 https://egghead.io/lessons/angularjs-testing-a-controller未知範圍提供者angularjs單元測試

所以基本上測試正在工作,但是一旦我將$ scope和$ rootScope注入控制器,它就會停止工作。

var myApp = angular.module("formulaViewer", []); 

myApp.controller("AppCtrl", function($scope, $rootScope) { 
    this.message = "Hello"; 
}) 

測試文件

describe("sup", function() { 
var appCtrl; 
beforeEach(module("formulaViewer")); 
beforeEach(inject(function ($controller) { 
    appCtrl = $controller("AppCtrl"); 
})); 

describe("AppCtrl", function() { 
    it("should pass", function() { 
     expect(appCtrl.message).toBe("Hello"); 
    }); 
}); 
}); 

我甚至嘗試添加$範圍,並在beforeEach $ rootScope(注(函數($控制器)的一部分,它仍然沒有奏效。

這是錯誤信息:

錯誤:[$ injector:unpr]未知提供者:$ scopeProvider < - $ scope http://errors.angularjs.org/1.2.26/ $注入器/ unpr?p0 =%24scopeProvider%20%3C-%20%24scope

回答

1

下面是我如何得到它的工作。

如果這在您的方案中不起作用,那麼您可以在注入$ rootScope並獲取該錯誤消息的位置顯示非工作測試?

describe("sup", function() { 
var appCtrl, $scope; // create the var for $scope 
beforeEach(module("formulaViewer")); 
beforeEach(inject(function ($controller, $rootScope) { // inject $rootScope 
    appCtrl = $controller("AppCtrl"); 
    $scope = $rootScope; // assign injected $rootScope to $scope 
})); 

describe("AppCtrl", function() { 
    it("should pass", function() { 
     expect(appCtrl.message).toBe("Hello"); 
    }); 
}); 
}); 

希望它有幫助。

相關問題