25

I'm using Jasmine for testing client & server side javascript and need to do some mocking. Has anyone got any suggestions for a good javascript mocking framework?

James Hollingworth
  • 14,040
  • 12
  • 39
  • 57

4 Answers4

10

I tried this once, but ended up refactoring instead so it wasn't needed. It doesn't have dependencies, so it should work just fine on node.js. http://sinonjs.org/

These are testing frameworks, but some of them include mocking frameworks internally: http://en.wikipedia.org/wiki/List_of_unit_testing_frameworks#JavaScript

Joshua Dale
  • 1,773
  • 3
  • 17
  • 25
8

I have not tried this one out(yet), but you could try node-gently created by Felix Geisendörfer. Right now I saw nodemock update at search.npmjs.org which is commited frequently also.

Quick search gave me:

Alfred
  • 60,935
  • 33
  • 147
  • 186
5

I'm using node-gently and it works great. Is not as complete as sinon but feels more confortable to work with.

masylum
  • 22,091
  • 3
  • 20
  • 20
-2

Why do you need a mocking framework? In statically-typed languages you need one because you can't change types at run-time.

I don't think you need this with JavaScript, the language is dynamic and powerful enough for you to not need this.

For example, I use backbone for my models and for the database connectivity. Backbone was designed well and has a single method Backbone.Sync that does all the database magic.

So I just do

var Backbone = require("backbone");
Backbone.Sync = function _mockedSync() {
  return ...;
}

You need to clarify exactly what you need. I don't think you need a mocking framework.

Raynos
  • 166,823
  • 56
  • 351
  • 396
  • 19
    Mocking frameworks, broadly, do 3 major things: 'fake object' creation, method stubbing and verification of expectations. Duck punching does let you do simple stubbing easily, but doesn't help with the other functionality. – ireddick Jul 14 '11 at 10:17
  • 1
    @ireddick you still have to do verification of expectations yourself, that's just duck typing and using `assert`. I call on YAGNI. Mocking when you have duck typing feels overengineered. – Raynos Jul 15 '11 at 01:18
  • 4
    @Raynos as ireddick said, what you showed is just stubbing. Check out this about the diff between mocks & stubs. http://martinfowler.com/articles/mocksArentStubs.html Some of the major missing pieces from duck punching is tracking that ONLY the interactions you want were called, and no others. and how many times each interaction was called, and with which parameters. yes you can build that yourself, but at that point you've just reinvented a mocking library. And even if you're only stubbing, a mocking framework is arguably more concise & readable than duck punching – Joseph Eames Feb 08 '12 at 23:37
  • 1
    @JosephEames I argue otherwise, stubbing by monkey patching is more concise and readable then mocking frameworks – Raynos Feb 09 '12 at 00:37
  • I've always hand rolled these mocks myself. – Sebastian Patten Nov 17 '13 at 01:51