Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Don't sinon.js spys catch errors?

So, I'm using mocha with chai to do my front-end testing, but I'm starting to incorporate sinon and really liking it. Except that testing throwing errors isn't working quite how the sinon docs seem to indicate.

Basically, I've got this method:

create: function(bitString, collectionType) {
    var collection;

    switch(collectionType) {
        case 'minutesOfHour':
            collection = this.createMinutesOfHour(bitString);
            break;

        case 'hoursOfDay':
            collection = this.createHoursOfDay(bitString);
            break;

        case 'daysOfWeek':
            collection = this.createDaysOfWeek(bitString);
            break;

        case 'daysOfMonth':
            collection = this.createDaysOfMonth(bitString);
            break;

        case 'monthsOfYear':
            collection = this.createMonthsOfYear(bitString);
            break;

        default:
            throw new Error('unsupported collection type ' + collectionType);
    }

    return collection;
},

and I'm testing it with this expectation:

it('throws error if missing second arguement', function() {
    sinon.spy(factory, 'create');

    factory.create();

    expect(factory.create).to.have.thrown();

    factory.create.restore();
});

however, the error, which I'm try to test for, also seems to halt the execution of the test

error message

I'd thought sinon.spy would include some try / catch logic internally, spy.throw doesn't seem as useful without it.

http://sinonjs.org/docs/#spies

Am I doing something wrong??

like image 552
noTxt Avatar asked Oct 22 '22 01:10

noTxt


1 Answers

I think one thing you could try is asserting against a spy object instead of the method, assign it to a variable. Not really knowing how sinon deals with all this exception magic...I think it might just work as you had expected.

it('throws error if missing second argument', function() {
  var spy = sinon.spy(factory, 'create');

  factory.create();

  expect(spy).to.have.thrown();

  factory.create.restore();
});

If that still doesn't work I think you could also do this test with standard chai if need be, leaving sinon out of the equation and actually gaining the check that the error has the correct message.

it('throws error if missing second argument', function() {
  expect(function() {
    factory.create();
  }).to.throw(/unsupported collection type/);
});

Or more concisely:

it('throws error if missing second argument', function() {
  expect(factory.create).to.throw(/unsupported collection type/);
});
like image 191
nackjicholson Avatar answered Oct 23 '22 20:10

nackjicholson