Testing Angular
  • Overview
  • Overview
  • 1. Create a new Angular CLI application
  • 2. Run default tests
  • 3. Review the Angular CLI directory and testing tools set up
  • 4. Jasmine tests
  • 5. Add simple test
  • 6. Add simple failing test
  • 7. Add another test to check a property
  • 8. Test the Component logic using a mocked up Service
  • 9. Test the Component logic using SpyOn
  • 10. TestBed and Fixtures
Powered by GitBook
On this page

7. Add another test to check a property

  • Remove the failing test

  • Under the last it block add another it block and write a test to check the title property is set correctly

src/app/app.component.spec.ts
import { AppComponent } from './app.component';
describe(`Component: App Component`, () => {
  it('add 1+1 - PASS', () => {
        expect(1 + 1).toEqual(2);

  });
  
  it(`title equals 'Angular Superpowers'`, () => {
    const component = new AppComponent();
    expect(component.title).toEqual('testing');
  });
});
  • set the title accordingly in the component

src/app/app.component.ts
export class AppComponent implements OnInit {
  title = 'testing';
  ...
}
  • Note that the AppComponent requires a CompanyService parameter. For now, we won't use it in the test though, so we just pass null.

  • Karma should still be running from your last test and on save of the spec file automatically re-run.

Previous6. Add simple failing testNext8. Test the Component logic using a mocked up Service

Last updated 6 years ago