user3715648
user3715648

Reputation: 1588

Trying to test Angular with Karma/Jasmine, but can't get the Jasmine tests to set $scope correctly

My Angular script looks like this:

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



app.controller('TestController', function ($scope) {
    $scope.test= "TEST";
});

My test file looks like this:

describe('first test', function() {
 var $scope;

  beforeEach(function (){
    module('myApp');

    inject(function($rootScope, $controller) {
        $scope = $rootScope.$new();
        ctrl = $controller('TestController', {
            $scope: $scope
        });
    });
  it('scope should have test', function() {
    expect($scope.test).toEqual("TEST");
  });
});

This test fails saying $scope.test is undefined. I debugged in Chrome and saw that $scope has a bunch of properties on it, but none of them are test. I've looked through several examples online, and they all look pretty similar to this. i'm not quite sure what i'm doing wrong here and i'm stuck....

edit I tried adding $controller to inject, but i'm still having the same problem.

Upvotes: 0

Views: 84

Answers (1)

alecxe
alecxe

Reputation: 474191

You need to pass $controller service alongside with $rootScope:

inject(function($rootScope, $controller) {
    $scope = $rootScope.$new();
    ctrl = $controller('TestController', {
        $scope: $scope
    });
});

Upvotes: 1

Related Questions