Giter VIP home page Giter VIP logo

nodemock's Introduction

Node Mock - Simple Yet Powerful Mocking Framework for NodeJs

NodeMock is a very simple to use mocking framework which can be used to mock functions in JavaScript objects. NodeMock creates mock methods in less code with more expressive manner.

I have passed the reins of maintenance for this project to Curtis Schlak. You can find new changes to it over at nodemock.

nodemock's People

Contributors

arunoda avatar aaronfay avatar dominictarr avatar gabrielfalcao avatar

Stargazers

Petros Bountis avatar Gabriel Almeida avatar Angus H. avatar Roger Castells avatar Aleksandr Vishniakov avatar Logan Koester avatar Renato Gama avatar Jay Chae avatar Soroush Mirzaei avatar Sébastien ELET avatar RegisM avatar Dru Riley avatar Baldo Alessandro avatar Alexandre Strzelewicz avatar green avatar Nelson Omuto avatar Dumitru Glavan avatar Oscar Renalias avatar Stefaney Roberts avatar kw avatar Yurii Shykanov avatar Marwan Hilmi avatar  avatar  avatar Lari Hoppula avatar Joakim Löfgren avatar Dan Oved avatar Muhammad Ghazali avatar Dario Diaz avatar teknopaul avatar Robert Kroeger avatar Sebastian Kusnier avatar Michael Dillon avatar Dick Hardt avatar Ruben Tan Long Zheng avatar David Guthu avatar Martin Wawrusch avatar Brian Brashear avatar thadclay avatar Amir H. Hajizamani avatar Harsha Siriwardena avatar Charles Davison avatar Richard Marr avatar Nikita Korotkih avatar  avatar Joshua Stauter avatar David R. Albrecht avatar Joshua Cohen avatar  avatar Ryan Gahl avatar hitoshi k. avatar Scott Elcomb avatar Sveinung Tord Røsaker avatar Ben Johnson avatar Rory Fitzpatrick avatar Hsu Ping Feng avatar Yoshihiro Sugi avatar Oleg Efimov avatar Bernhard Weisshuhn (a.k.a. bernhorst) avatar  avatar  avatar Daniel Gasienica avatar Tom de Grunt avatar Chris Hamant avatar Andrew Montgomery avatar Jaime Bueza avatar  avatar Wade Simmons avatar Scott Noel-Hemming avatar Daniel O'Shea avatar John D. Mitchell avatar Alex Robinson avatar Charlie Robbins avatar Charleno Pires avatar Robert Malko avatar

Watchers

Viktor Trako avatar Richard Hess avatar  avatar James Cloos avatar  avatar

Forkers

rasata

nodemock's Issues

assertThrows should raise a meaningful error message

assertThrows always raises the same error:

Nodemock rules breaked!

whereas assert writes to the console:

method call for: '" + method + "()' with params: " + getParamString(entry.args) + " was not executed!

Both methods should share the same description. assert should print it, assertThrows should raise it.

mock.assert() does not throw...

I would expect a function named assert to throw.

i'll get a pull request ready for this if you like, unless you have a particular reason for doing it this way?

How do I mock functions?

How do I use nodemock to mock functions?

If I do

var fn = nodemock.mock('foo')

I will get

fn.foo()

But what I actually want is

fn()

Enable a name to be assigned to a mock

Currently the only descriptive name on a mock is the function being mocked.

However if two mocks are both mocking the same function name (e.g. 'error') there isn't a way to tell from the assert error message which 'error' function was not called.

Mocking event emitters

How do I mock event emitters? In my current project I use a factory pattern to achieve dependency injection on all modules (every module has a make() function that is basically a constructor injection thingy), and I have no trouble mocking NodeJS core API but have no idea how to mock event emitters (other than writing my own).

How does nodemock do this?

Unit tests are failing

Following test methods are failing:

✔ testMultipleEntriesForOneMethod
method call for: 'foo()' with params: (10, 30) was not executed!

✔ testAssertFailsSameMethod
method call for: 'bar()' with params: (10, 30) was not executed!

✔ testAssertOK
method call for: 'bar()' with params: (10, 30) was not executed!

✔ testAssertThrowsOK
method call for: 'bar()' with params: (10) was not executed!

BUG: leaking entry global var

  • is the leak of entry var in global scope intentional, or is it a genuine mistake? I think its a mistake.

file: lib/nodemock.js, line 40, branch master.

entry = entries[method][index];

  • thanks for this awesome lib.

cannot assign a mocked function to a var

var nm = require('nodemock')

var m = nm.mock('foo').takes(10)
var foo = m.foo

foo(10)

this will throw

Error: Mock function 'foo()' is not defined
  at /home/dominic/source/dev/experiement/node_modules/nodemock/lib/nodemock.js:56:11
  at /home/dominic/source/dev/experiement/node_modules/nodemock/lib/nodemock.js:56:11

I use functions by them selves all the time, would be handy to be able to mock them. instead I have to do this:

var foo = function () { m.foo.apply(m,[].slice.call(arguments) }

which is not as nice.

No way to mock multiply-called method with differing return values

In the current version, there is no way to return different values from a single multiply-called mock. Example:

mocked.mock('getBlah').return('one');
mocked.mock('setBlah').takes(2);
mocked.mock('getBlah').return('two');

// ...test stub...

test.equal(mocked.getBlah(), 'one', 'should be one');
mocked.setBlah(2);
test.equal(mocked.getBlah(), 'two', 'should be two');   // << fails --- getBlah returns 'one' again.

test.ok(mocked.assert(), 'bad/wrong mock order');

times() shouldn't be default

I'm a little confused about why times() is defaulted to 1 for all mock functions. I mean, in unit tests I generally don't need times() at all. The only time I use it is when I am testing collection objects, but in most of my unit test cases I do call the same function with the same parameters multiple times to check for static property contamination, but there's no upper limit to how many times I call the same code.

Therefore I think a function shouldn't have "One time use only" by default.

Feature: assertThrows should not succeed when a "marked to fail" method was called on mock

  • I really am missing this feature.
  • I have marked a method to fail. And need to assert the fact in test that that method was never called.
  • In this particular case, the mock throws an error, but the SUT catches it, since its running a loop, with try/catch in the loop.
  • Thus my tests are all passing. :(
  • Ideally if assertThrows would throw an exception when any method was called on mock that was marked for failure, this wouldn't let my test pass.
  • Is it possible to easily integrate this feature?

thanks.

Assert should throw error to be consistent with rest of methods

All other methods besides assert throw errors when something happens that is not expected. Assert writes to console.log:

this.assert = function() {

    var success = true;
    for(var method in entries) {
        var entriesForMethod = entries[method];
        entriesForMethod.forEach(function(entry) {
            if(!entry.shouldFail && entry.executed == false) {
                success = false;
                console.error(
                        "method call for: '" + method + "()' with params: " + getParamString(entry.args) + " was not executed!\n"
                );
            }
        });
    }

    return success;
};

To be consistent, Assert should throw an Error

fail() throws exception on assertThrows when method was not called

When you add a method to the mock that you explicitely don't want to be called, but afterwards call mock.assertThrows, becouse there was another method that did have to be called, fail() throws an error, even if that method was not called.

I wrote a little test to illustrate the problem (it fails):

exports.testFailThrowsNoExceptionWhenNotCalled = function(test){
var mock = nm.mock('test').fail();
test.doesNotThrow(function() {
// the method was not called so, no exception should be thrown
mock.assertThrows();
});
test.done();
}

A way to mock chains

If a method returns this you can chain it's function calls

thing.foo().bar() 

but you can't go

mock = nodemock.mock('foo').returns(mock)

one way to support this would be to ab able to add more functions to a mock

mock = nodemock.mock('foo').returns(mock)
nodemock.more(mock).mock('chain').returns(mock)

cheers. Dominic

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.