// App class TestService { public name: string = 'Injected Service'; } // App tests import { it, describe, expect, inject, beforeEachProviders, } from 'angular2/testing'; import { provide } from 'angular2/core'; import { setBaseTestProviders } from 'angular2/testing'; import { TEST_BROWSER_PLATFORM_PROVIDERS, TEST_BROWSER_APPLICATION_PROVIDERS, } from 'angular2/platform/testing/browser'; setBaseTestProviders(TEST_BROWSER_PLATFORM_PROVIDERS, TEST_BROWSER_APPLICATION_PROVIDERS); describe('TestService', () => { beforeEach(function() { this.testService = new TestService(); }); it('should have name property set', function() { expect(this.testService.name).toBe('Injected Service'); }); }); describe('TestService Injected', () => { beforeEachProviders(() => [TestService]); it('should have name property set', inject([TestService], (testService: TestService) => { expect(testService.name).toBe('Injected Service'); })); }); class MockTestService { public mockName: string = 'Mocked Service'; } describe('TestService Mocked', () => { beforeEachProviders(() => [ provide(TestService, {useClass: MockTestService}) ]); it('should have name property set', inject([TestService], (testService: TestService) => { expect(testService.mockName).toBe('Mocked Service'); })); }); class MockTestServiceInherited extends TestService { public sayHello(): string { return this.name; } } describe('TestService Mocked Inherited', () => { beforeEachProviders(() => [ provide(TestService, { useClass: MockTestServiceInherited }) ]); it('should say hello with name', inject([TestService], (testService: TestService) => { expect(testService.sayHello()).toBe('Injected Service'); })); });