Skip to content

Instantly share code, notes, and snippets.

@chris2cant
Last active October 1, 2019 16:12
Show Gist options
  • Save chris2cant/9720bf1c5b89fca0061377cca8233118 to your computer and use it in GitHub Desktop.
Save chris2cant/9720bf1c5b89fca0061377cca8233118 to your computer and use it in GitHub Desktop.

Angular Testing

SpyOn

// Should call
it('Should call myFunc', () => {
  const spiedFunction = spyOn<any>(component, 'myFunc');
  component.myMethod();
  expect(spiedFunction).toHaveBeenCalled();
});

// Should not call
it('Should not call myFunc', () => {
  const spiedFunction = spyOn<any>(component, 'myFunc');
  component.myMethod();
  expect(spiedFunction).not.toHaveBeenCalled();
});

External function

import * as dateHelper from './date.helper';

it('Should call dayIsValid', () => {
  const spiedFunction = spyOn<any>(
    dateHelper,
    'isValid'
  ).and.returnValue({});
  component.myMethod();
  expect(spiedFunction).toHaveBeenCalled();
});

Private method

it('Should call myFunc', () => {
  const spiedFunction = spyOn<any>(component, 'myFunc');
  component.myMethod();
  expect(spiedFunction).toHaveBeenCalled();
});

Tools

Articles

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment