【问题标题】:Unknown scope provider angularjs unit test未知范围提供者 angularjs 单元测试
【发布时间】:2014-12-06 03:23:17
【问题描述】:

我尝试做一些与 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(inject(function ($controller) 部分中添加 $scope 和 $rootScope,但仍然不起作用。

这是错误信息:

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

【问题讨论】:

    标签: angularjs unit-testing


    【解决方案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");
        });
    });
    });
    

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-02
      • 2015-03-04
      • 1970-01-01
      • 1970-01-01
      • 2015-04-03
      • 2017-12-21
      相关资源
      最近更新 更多