Students Save 30%! Learn & create with unlimited courses & creative assets Students Save 30%! Save Now

1.2 Testing Angular Components

In this lesson we’ll add some basic unit tests for an existing Angular component using Jasmine, and then run them from the command line with Grunt and Karma.

To easily get an instance of the controller attached to the component, we can use the $componentController service. We can inject it into our tests using the inject method and save it to a variable. We wrap a service in underscores when injecting it, and Angular knows to unwrap it for us. See the code snippet below for an example.

Code Snippet

beforeEach(inject(function (_$rootScope_, _$componentController_) {
    $rootScope = _$rootScope_;
    $scope = $rootScope.$new();
    $componentController = _$componentController_;

    sandbox = sinon.sandbox.create();
}));

Related Links

1.Testing Angular Components
2 lessons, 17:48

Free Lesson
1.1
Introduction
00:34

1.2
Testing Angular Components
17:14