Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

deride

Package Overview
Dependencies
Maintainers
2
Versions
32
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

deride

Mocking library based on composition

  • 0.3.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
65
increased by242.11%
Maintainers
2
Weekly downloads
 
Created
Source

deride Build Status NPM version Dependency Status Stories in Ready Stories In Progress

NPM

Mocking library based on composition

The inspiration for this was that my colleague was having a look at other mocking frameworks and mentioned to me that they do not work when using Object.freeze in the objects to enforce encapsulation. This library builds on composition to create a mocking library that can work with objects which are frozen.

Getting Started

Install the module with: npm install deride

var deride = require('deride');

Documentation

Mocking

  • deride.wrap(obj)

CAUTION Remember when you use this function about the good practice recommended in the book Growing Object-Oriented Software, Guided by Tests Chapter 8: Only Mock Types That You Own

Expectations

**All of the above can be negated e.g. negating the .withArgs would be: **

  • obj.expect.method.called.not.withArgs(args)

Resetting the counts / called with args

Setup

Examples

The context

var Person = function(name) {
    return Object.freeze({
        greet: function(otherPersonName) {
            console.log(name, 'says hello to', otherPersonName);
        },
		echo: function(name) {
			return name;
		}
    });
}

Creating a stubbed object

Stubbing an object simply creates an anonymous object, with all the method specified and then the object is wrapped to provide all the expectation functionality of the library

var bob = deride.stub(['greet']);
bob.greet('alice');
bob.expect.greet.called.times(1);

Creating a stubbed object based on an existing object

var Person = {
    greet: function(name) {
        return 'alice sas hello to ' + name;
    },
};
var bob = deride.stub(Person);
bob.greet('alice');
bob.expect.greet.called.once();

Creating a single mocked method

var func = deride.func();
func.setup.toReturn(1);
var value = func(1, 2, 3);
assert.equal(value, 1);

Events

Force the emit of an event on an object

var bob = deride.stub([]);
bob.on('message', function() {
    done();
});
bob.emit('message', 'payload');

Emit an event on method invocation

bob.setup.greet.toEmit('testing');
bob.on('testing', function() {
	done();
});
bob.greet('bob');

Emit an event with args on method invocation

bob.setup.greet.toEmit('testing', 'arg1', { a: 1 });
bob.on('testing', function(a1, a2) {
	a1.should.eql('arg1');
	a2.should.eql({ a: 1 });
	done();
});
bob.greet('bob');

Count the number of invocations of a method

var bob = new Person('bob');
bob = deride.wrap(bob);
bob.greet('alice');
bob.expect.greet.called.times(1);

Has convenience methods for invocation counts

var bob = new Person('bob');
bob = deride.wrap(bob);
bob.greet('alice');
bob.expect.greet.called.once();
bob.greet('sally');
bob.expect.greet.called.twice();

Determine if a method has never been called

var bob = new Person('bob');
bob = deride.wrap(bob);
bob.expect.greet.called.never();

Resetting the called count on all methods

var bob = new Person('bob');
bob = deride.wrap(bob);
bob.greet('alice');
bob.echo('alice');
bob.expect.greet.called.once();
bob.expect.echo.called.once();

bob.called.reset();

bob.expect.greet.called.never();
bob.expect.echo.called.never();

Determine if a method was called with a specific set of arguments

var bob = new Person('bob');
bob = deride.wrap(bob);
bob.greet('alice');
bob.greet('bob');
bob.expect.greet.called.withArgs('bob');

Override the method body to change the invocation

var bob = new Person('bob');
bob = deride.wrap(bob);
bob.setup.greet.toDoThis(function(otherPersonName) {
    return util.format('yo %s', otherPersonName);
});
var result = bob.greet('alice');
result.should.eql('yo alice');

Override the return value for a function

var bob = new Person('bob');
bob = deride.wrap(bob);
bob.setup.greet.toReturn('foobar');
var result = bob.greet('alice');
result.should.eql('foobar');

Overriding the promise resolver for a function

To resolve with a value
var bob = new Person('bob');
bob = deride.wrap(bob);
bob.setup.greet.toResolveWith('foobar');
bob.greet('alice').then(function(result) {
    result.should.eql('foobar');
});
To reject with a value
var bob = new Person('bob');
bob = deride.wrap(bob);
bob.setup.greet.toRejectWith('foobar');
bob.greet('alice').catch(function(result) {
    result.should.eql('foobar');
});

Force a method invocation to throw a specific error

var bob = new Person('bob');
bob = deride.wrap(bob);
bob.setup.greet.toThrow('BANG');
should(function() {
    bob.greet('alice');
}).
throw(/BANG/);

Override the invocation of a callback

var bob = new Person('bob');
bob = deride.wrap(bob);
bob.setup.chuckle.toCallbackWith([0, 'boom']);
bob.chuckle(function(err, message) {
    assert.equal(err, 0);
    assert.equal(message, 'boom');
});

Accelerating the timeout used internally by a function

var Person = function(name) {
    return Object.freeze({
        foobar: function(timeout, callback) {
            setTimeout(function() {
                callback('result');
            }, timeout);
        }
    });
};
var timeout = 10000;
var bob = new Person('bob');
bob = deride.wrap(bob);
bob.setup.foobar.toTimeWarp(timeout);
bob.foobar(timeout, function(message) {
    assert.equal(message, 'result');
});

Setup for specific arguments

Setting the return value of a function when specific arguments are used

var bob = new Person('bob');
bob = deride.wrap(bob);
bob.setup.greet.when('alice').toReturn('foobar');
bob.setup.greet.toReturn('barfoo');
var result1 = bob.greet('alice');
var result2 = bob.greet('bob');
result1.should.eql('foobar');
result2.should.eql('barfoo');

Overriding a method`s body when specific arguments are provided

var bob = new Person('bob');
bob = deride.wrap(bob);
bob.setup.greet.when('alice').toDoThis(function(otherPersonName) {
    return util.format('yo yo %s', otherPersonName);
});
bob.setup.greet.toDoThis(function(otherPersonName) {
    return util.format('yo %s', otherPersonName);
});
var result1 = bob.greet('alice');
var result2 = bob.greet('bob');
result1.should.eql('yo yo alice');
result2.should.eql('yo bob');

Throwing an error for a method invocation when specific arguments are provided

var bob = new Person('bob');
bob = deride.wrap(bob);
bob.setup.greet.when('alice').toThrow('BANG');
should(function() {
    bob.greet('alice');
}).
throw (/BANG/);
should(function() {
    bob.greet('bob');
}).not.
throw (/BANG/);

Override the invocation of a callback when specific arguments are provided

var bob = new Person('bob');
bob = deride.wrap(bob);
bob.setup.chuckle.toCallbackWith([0, 'boom']);
bob.setup.chuckle.when('alice').toCallbackWith([0, 'bam']);
bob.chuckle(function(err, message) {
    assert.equal(err, 0);
    assert.equal(message, 'boom');
    bob.chuckle('alice', function(err, message) {
        assert.equal(err, 0);
        assert.equal(message, 'bam');
    });
});

Accelerating the timeout used internally by a function when specific arguments are provided

var Person = function(name) {
    return Object.freeze({
        foobar: function(timeout, callback) {
            setTimeout(function() {
                callback('result');
            }, timeout);
        }
    });
};
var timeout1 = 10000;
var timeout2 = 20000;
var bob = new Person('bob');
bob = deride.wrap(bob);
bob.setup.foobar.toTimeWarp(timeout1);
bob.setup.foobar.when(timeout2).toTimeWarp(timeout2);
bob.foobar(timeout1, function(message) {
    assert.equal(message, 'result');
    bob.foobar(timeout2, function(message) {
        assert.equal(message, 'result');
        done();
    });
});

Provide access to individual calls to a method

var bob = deride.wrap(bob);
bob.greet('jack', 'alice');
bob.greet('bob');
bob.expect.greet.invocation(0).withArg('alice');
bob.expect.greet.invocation(1).withArg('bob');

Enable the assertion on a single arg being used in any invocation

when the arg is a primitive object

var bob = deride.wrap(bob);
bob.greet('alice', {
    name: 'bob',
    a: 1
}, 'sam');
bob.expect.greet.called.withArg('sam');

when the arg is not a primitive object

var bob = deride.wrap(bob);
bob.greet('alice', {
    name: 'bob',
    a: 1
});
bob.expect.greet.called.withArg({
    name: 'bob'
});

Contributing

Please ensure that you run grunt, have no style warnings and that all the tests are passing.

License

Copyright (c) 2014 Andrew Rea
Copyright (c) 2014 James Allen

Licensed under the MIT license.

Keywords

FAQs

Package last updated on 12 Jun 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc