Skip to content

dlangevin/qunit-mock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

QUnit Mock

QUnit Mock provides some mock assertions and stubs for QUnit.

Installation

Just download the qunit.mock.js and include it after QUnit.

Expecting calls

See an example of expecting calls:

MyClass = function() {}

MyClass.prototype = {
  methodOne: function() {
    this.methodTwo();
  },
  
  methodTwo: function() {},
  
  methodThree: function() {
    this.methodTwo();
    this.methodTwo();
  }
};

test("calls second method on first", function() {
  obj = new MyClass();
  expectCall(obj, "methodTwo"); // expect the method call
  obj.methodOne();
});

test("calls second method two times on third", function() {
  obj = new MyClass();
  expectCall(obj, "methodTwo", 2); // this time saying number of expect calls
  obj.methodThree();
});

At end of test it will automatic check if the method is called and will test it.

Stubbing

See an example of stubbing methods:

MyClass = function() {}

MyClass.prototype = {
  someMethod: function() { return "hello"; }
}

obj = new MyClass();

test("stubbing method", function() {
  stub(obj, "someMethod", function() { return "world" });
  equal(obj.someMethod(), "world"); // ok, will pass
});

test("method after stub method", function() {
  equal(obj.someMethod(), "hello"); // ok too
});

Stubbing is easy, also, note in second test the method returned to original state, after the test finish all stubs will be removed and object will keep original ones.