Skip to content

Instantly share code, notes, and snippets.

@Krxtopher
Forked from tobius/omit-callback-unit-test.js
Last active December 26, 2015 15:18

Revisions

  1. Krxtopher revised this gist Oct 26, 2013. 1 changed file with 11 additions and 11 deletions.
    22 changes: 11 additions & 11 deletions omit-callback-unit-test.js
    Original file line number Diff line number Diff line change
    @@ -5,25 +5,25 @@ exist in the should.js, expect.js, and chai assertion libraries which work with

    var makeSomething = function(callback) {
    // Below is the simplest implementation that will result in the test passing.
    // If you want a more robust implementation, you'll want to write more tests first. :)
    throw(new Error("Missing parameters"));
    // If you want a more robust implementation, you'll want to write more tests first. :)
    throw(new Error("Missing parameters"));
    };

    describe("makeSomething()", function() {

    describe("when called with no callback parameter", function() {
    describe("when called with no callback parameter", function() {

    it("should throw a meaningful error", function() {
    it("should throw a meaningful error", function() {

    expect(function() {
    makeSomething();
    }).toThrow(new Error("Missing parameters"));
    expect(function() {
    makeSomething();
    }).toThrow(new Error("Missing parameters"));

    // Note, you can also use toThrow() with no params if you don't care about matching
    // the exact error message.
    // Note, you can also use toThrow() with no params if you don't care about matching
    // the exact error message.

    });
    });

    });
    });

    });
  2. Krxtopher revised this gist Oct 26, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion omit-callback-unit-test.js
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ exist in the should.js, expect.js, and chai assertion libraries which work with
    */

    var makeSomething = function(callback) {
    // Below is the simplest implementation that will result in the test passing.
    // Below is the simplest implementation that will result in the test passing.
    // If you want a more robust implementation, you'll want to write more tests first. :)
    throw(new Error("Missing parameters"));
    };
  3. Krxtopher revised this gist Oct 26, 2013. 1 changed file with 19 additions and 57 deletions.
    76 changes: 19 additions & 57 deletions omit-callback-unit-test.js
    Original file line number Diff line number Diff line change
    @@ -1,67 +1,29 @@
    // node module
    var assert = require('better-assert');

    /**
    * make something
    *
    * @param {Function} callback(err, something)
    */
    var makeSomething = function(callback){

    var something = 1;

    if (arguments.length >= 1){

    something++;
    callback(err, something);

    } else {

    // nothing = fail
    // do not throw error = fail
    // do not throw custom error message = fail

    // throw custom error message = pass
    throw new Error('Missing parameters');

    }

    /*
    The implementation below assumes you're using Jasmine's built-in matchers. But similar matchers
    exist in the should.js, expect.js, and chai assertion libraries which work with Mocha.
    */

    var makeSomething = function(callback) {
    // Below is the simplest implementation that will result in the test passing.
    // If you want a more robust implementation, you'll want to write more tests first. :)
    throw(new Error("Missing parameters"));
    };

    describe('makeSomething()', function(){

    it('should be a function', function(){

    assert(makeSomething !== undefined, 'must not be undefined');
    assert(typeof makeSomething === 'function', 'must be a function');

    });

    describe('making something to callback to nothing', function(){

    it('should throw a meaningful error', function(done){

    var err;

    try {

    makeSomething();

    } catch(e){
    describe("makeSomething()", function() {

    err = e;
    assert(err !== undefined, 'must throw a catchable error');
    assert(err.toString() !== 'Error', 'must throw a meaningful error');
    describe("when called with no callback parameter", function() {

    } finally {
    it("should throw a meaningful error", function() {

    assert(err !== undefined, 'must throw an error');
    done();
    expect(function() {
    makeSomething();
    }).toThrow(new Error("Missing parameters"));

    }
    // Note, you can also use toThrow() with no params if you don't care about matching
    // the exact error message.

    });
    });

    });
    });

    });
  4. @tobius tobius created this gist Oct 25, 2013.
    67 changes: 67 additions & 0 deletions omit-callback-unit-test.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,67 @@
    // node module
    var assert = require('better-assert');

    /**
    * make something
    *
    * @param {Function} callback(err, something)
    */
    var makeSomething = function(callback){

    var something = 1;

    if (arguments.length >= 1){

    something++;
    callback(err, something);

    } else {

    // nothing = fail
    // do not throw error = fail
    // do not throw custom error message = fail

    // throw custom error message = pass
    throw new Error('Missing parameters');

    }

    };

    describe('makeSomething()', function(){

    it('should be a function', function(){

    assert(makeSomething !== undefined, 'must not be undefined');
    assert(typeof makeSomething === 'function', 'must be a function');

    });

    describe('making something to callback to nothing', function(){

    it('should throw a meaningful error', function(done){

    var err;

    try {

    makeSomething();

    } catch(e){

    err = e;
    assert(err !== undefined, 'must throw a catchable error');
    assert(err.toString() !== 'Error', 'must throw a meaningful error');

    } finally {

    assert(err !== undefined, 'must throw an error');
    done();

    }

    });

    });

    });