0

I have class that hold's property array and some functionality in it. The class has remove method that removes number of the specific index:

class SortedList {
    constructor() {
        this.list = [];
    }

    add(element) {
        this.list.push(element);
        this.sort();
    }

    remove(index) {
        this.vrfyRange(index);
        this.list.splice(index, 1);
    }

I make Mocha tests for this class and i want to throw error when parameter of remove functionality are negative number or greater than the array size. Problem is that I can't get an error message. I try the following:

it('check for incorrect input', function () {
            sorted.add(2);
            sorted.add(3);

            expect(sorted.remove(-1)).to.throw(Error('Index was outside the bounds of the collection.'))
        });
Can someone help me with this?
V.Dimitrov
  • 99
  • 1
  • 5
  • Your code does not explicitly throw an exception, so I wouldn't expect your assertion to pass. Wrap the code inside remove() in a try block if you want it, not the runtime, to throw the error. – kinakuta Oct 31 '16 at 22:58
  • The try-catch work's fine for me, thanks. – V.Dimitrov Oct 31 '16 at 23:19

1 Answers1

0

Pass the function that expects to throw an Error to a lambda and the error message as the second argument to throw function.

it('check for incorrect input', function () {
  list.add(2);
  list.add(3);

  expect(() => list.remove(-1)).to.throw(Error, 'Index was outside the bounds of the collection.')
});
mr.d
  • 386
  • 4
  • 18