public
Description: A mocking and stubbing framework for Javascript
Homepage:
Clone URL: git://github.com/gisikw/jsocka.git
Click here to lend your support to: jsocka and make a donation at www.pledgie.com !
gisikw (author)
Tue Aug 04 10:39:00 -0700 2009
commit  5544b2f53020f784475ba21ded79c8bf896f6b34
tree    91c4f4707a1bcf0517270fc40d449168a90f4e60
parent  fca0e9e0992f14d7386e635d6b79883756d8c9ec
jsocka /
name age message
file History.rdoc Loading commit data...
file LICENSE
file README.rdoc
directory lib/
directory modules/
directory spec/
README.rdoc

JSocka

JSocka is a library for stubbing methods and parameters in Javascript. It strives to replicate Mocha syntax, which in turn uses a JMock-like syntax.

JSocka is designed to be test-framework-agnostic, though the test specs for the code itself are written in JSpec. JSocka also includes an integration module for JSpec, which provides alternative syntax.

For more information regarding the JSpec testing framework, please visit github.com/visionmedia/jspec/tree/master

Getting Started

In order to start using JSocka, all you need to do is include the lib/jsocka.js Javascript file.

Here are some usage examples

  JSocka("Person").stubs("speak").returns("function(){alert('I love stubbing')});
  // Person.speak() now calls the stubbed function

  JSocka("Person").any_instance.stubs("speak").returns("function(){alert('I love stubbing')});
  // p = Person.new
  // p.speak() now calls the stubbed function

  JSocka("Person").expects("speak");

  JSocka("Person").expects("speak").never();

  JSocka("Person").expects("speak").returns("function(){alert('I love stubbing')});

  JSocka.clearStubs(); // This clears out all existing stubs, and resets the original functions

Usage Notes

Please note that every JSocka method is a function, not a parameter. While Mocha does support some syntax like Person.expects(method).once, there’s not a way to implement this in Javascript. Everything needs parentheses! The only exception to this is when using JSocka("Object").any_instance.stubs()

Expectations are destructive (Adding hook code with chaining doesn’t work for base classes). Be aware that JSocka("Person").expects("speak") will keep track of how many times the Person.speak method is called, but Person.speak will not return anything unless you explicitly define it, i.e. JSocka("Person").expects("speak").returns("function(){ // code}")

JSpec Integration

The modules/jspec.jsocka.js file is a module that extends JSpec functionality. This includes:

  • New Object.stubs syntax, instead of JSocka("Object").stubs. This works with expects and any_instance as well.
  • Automatic expectation-checking and destubbing after every spec. For example, the following spec will automatically fail:
     describe "Example"
       it "should fail"
         Object.expects("my_method")
       end
     end
    

JSpec will automatically hook into the errors collection and display them with the test.

Bugs & Feature Requests

Comments & Suggestions

Please feel free to contact me with questions or suggestions at kevin.gisi@gmail.com