$scopeProvider <- $scope/ Unknown provider

99封情书 提交于 2019-12-03 12:08:07

问题


I testing my angular-application with jasmine(http://jasmine.github.io/2.0/) and getting next error: Unknown provider: $scopeProvider <- $scope I know, that it's incorrect to build dependency with scope in filters, services, factories, etc., but I use $scope in controller! Why am i getting this error? controller looks like

testModule.controller('TestCont', ['$filter', '$scope', function($filter, $scope){

        var doPrivateShit = function(){
            console.log(10);
        };

        this.lol = function(){
            doPrivateShit();
        };

        this.add = function(a, b){
            return a+b;
        };

        this.upper = function(a){
            return $filter('uppercase')(a);
        }   

        $scope.a = this.add(1,2);

        $scope.test = 10;

        $scope.search = {

        };
    }]);

and my test's code:

'use strict';

describe('testModule module', function(){
    beforeEach(function(){
        module('testModule');
    });

    it('should uppercase correctly', inject(function($controller){
        var testCont = $controller('TestCont');
        expect(testCont.upper('lol')).toEqual('LOL');
        expect(testCont.upper('jumpEr')).toEqual('JUMPER');
        expect(testCont.upper('123azaza')).toEqual('123AZAZA');
        expect(testCont.upper('111')).toEqual('111');
    }));
});

回答1:


You need to manually pass in a $scope to your controller:

describe('testModule module', function() {
    beforeEach(module('testModule'));

    describe('test controller', function() {
        var scope, testCont;

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

        it('should uppercase correctly', function() {
            expect(testCont.upper('lol')).toEqual('LOL');
            expect(testCont.upper('jumpEr')).toEqual('JUMPER');
            ...
        });
    });
});



回答2:


Normally, a $scope will be available as an injectable param only when the controller is attached to the DOM.

You need to associate somehow the controller to the DOM (I'm mot familiar with jasmine at all).




回答3:


I am following a video tutorial from egghead (link bellow) which suggest this approach:

describe("hello world", function () {
    var appCtrl;
    beforeEach(module("app"))
    beforeEach(inject(function ($controller) {
        appCtrl = $controller("AppCtrl");
    }))

    describe("AppCtrl", function () {
        it("should have a message of hello", function () {
            expect(appCtrl.message).toBe("Hello")
        })
    })
})

Controller:

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

app.controller("AppCtrl",  function () {
    this.message = "Hello";
});

I am posting it because in the answer selected we are creating a new scope. This means we cannot test the controller's scope vars, no?

link to video tutorial (1min) : https://egghead.io/lessons/angularjs-testing-a-controller



来源:https://stackoverflow.com/questions/26591402/scopeprovider-scope-unknown-provider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!