L
L
lMoJlo4HuKl2018-03-27 13:18:31
Angular
lMoJlo4HuKl, 2018-03-27 13:18:31

How to get scope or $ctrl in angularjs tests?

How to get a scope or $ctrl or at least something from a component (its controller) in a test?

app.component('authComponent', {
    templateUrl: 'app/auth/auth.html',
    controller: authCtrl
});

function authCtrl()
 var $ctrl = this;
 this.data = [1,2,3];
}

Test :
describe('component: heroDetail', function() {

  var $componentController;

  beforeEach(module('globalApp'));
  beforeEach(inject(function(_$componentController_) {
    $componentController = _$componentController_;
  }));

  it('should expose a `hero` object', function() {

    var ctrP = $componentController('authCtrl');

    expect(ctrP.data).toBeDefined();

  });
});

Mistake:
Unknown provider: authCtrlDirectiveProvider <- authCtrlDirective

In this case, ctrP = undefined

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nicholas, 2018-03-28
@healqq

You need to get the controller by the name of the component, i.e.
var ctrP = $componentController('authComponent');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question