$scopeProvider <- $scope/ Unknown provider

后端 未结 3 562
执笔经年
执笔经年 2021-02-05 10:02

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

相关标签:
3条回答
  • 2021-02-05 10:06

    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).

    0 讨论(0)
  • 2021-02-05 10:28

    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

    0 讨论(0)
  • 2021-02-05 10:31

    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');
                ...
            });
        });
    });
    
    0 讨论(0)
提交回复
热议问题