T
T
tostershmoster2021-09-22 09:15:09
Unit testing
tostershmoster, 2021-09-22 09:15:09

How to test a private static method that throws an exception?

constructor(settings: ISettings) {
  Model.validateSettings(settings);
  this.settings = settings;
}

private static validateSettings(settings: ISettings): void {
  if (settings.min >= settings.max) {
    throw new Error("'max' must be greater than 'min'");
  }
}


test
let settings: ISettings;

beforeEach(() => {
  settings = {
    min: 0,
    max: 1500,
    isTwoRunners: true,
    isScaleVisible: true,
    isVertical: false,
    isTooltipsVisible: true,
    valueFrom: 1000,
    valueTo: 1490,
    step: 10,
  };
});

describe('function validateSettings: ', () => {
  test('"settings.min >= settings.max" should throw Error', () => {
    settings.min = 2000;
    settings.max = 1500;

    expect(Model['validateSettings'](settings)).toThrow("'max' must be greater than 'min'");
  });
});


mistake
614ac9291344e823629401.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexandroppolus, 2021-09-22
@tostershmoster

expect(() => Model['validateSettings'](settings)).toThrow("'max' must be greater than 'min'");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question