Ratko Ratko - 4 months ago 45
Javascript Question

Jasmine Spies.and.stub method

I've been reading through the Jasmine documentation and I've been struggling to understand what the Spies

method actually does. English is not my native language, so I don't even know what the word "stub" actually means, and there is no translation for it in my language.

In the documentation it says:

When a calling strategy is used for a spy, the original stubbing behavior can be returned at any time with and.stub.

describe("A spy", function() {

var foo, bar = null;

beforeEach(function() {
foo = {
setBar: function(value) {
bar = value;

spyOn(foo, 'setBar').and.callThrough();

it("can call through and then stub in the same spec", function() {

bar = null;


What does
actually do and how is it useful?


For the term, you can look at wikipedia : http://en.wikipedia.org/wiki/Test_stub

In a nutshell it's a "fake" object that you can control that replaces a "real" object in your code.

For the function, what I understand is that and.stub() removes the effect of and.callThrough() on a spy.

When you call and.callThrough, the spy acts as a proxy, calling the real function, but passing through a spy object allowing you to add tests like expectation.

When you call and.stub, or if you never call and.callThrough, the spy won't call the real function. It's really usefull when you don't want to test an object's behavior, but be sure that it was called. Helping you to keep your test truly unitary.