$scopeProvider <- $scope/ Unknown provider

妖精的绣舞 提交于 2019-12-03 01:42:42
Swoogan

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');
            ...
        });
    });
});

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

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

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