2014-10-10 97 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"); 
    }); 
}); 
}); 

希望它有帮助。

相关问题