未知范围提供者angularjs单元测试

时间:2014-10-10 21:20:36

标签: angularjs unit-testing

我尝试做一些与egghead.io上的例子非常相似的事情。 https://egghead.io/lessons/angularjs-testing-a-controller

所以基本上测试工作正常,但是一旦我将$ 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中添加$ scope和$ rootScope(inject(函数($ controller)部分,它仍然没有工作。

这是错误消息:

错误:[$ injector:unpr]未知提供者:$ scopeProvider< - $ scope     http://errors.angularjs.org/1.2.26/ $注射器/ unpr?P0 =%24scopeProvider%20%3 C-%20%24scope

1 个答案:

答案 0 :(得分: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");
    });
});
});

希望它有所帮助。

相关问题