Class Index | File Index

Classes


Class jasmine.Spy


Defined in: jasmine-0.9.0.js.

Class Summary
Constructor Attributes Constructor Name and Description
 
jasmine.Spy(name)
Jasmine Spies are test doubles that can act as stubs, spies, fakes or when used in an expecation, mocks.
Field Summary
Field Attributes Field Name and Description
 
Holds arguments for each call to the spy, indexed by call count
 
The name of the spy, if provided.
 
Is this Object a spy?
 
Tracking of the most recent call to the spy.
Method Summary
Method Attributes Method Name and Description
 
andCallFake(fakeFunc)
Calls an alternate implementation when a spy is called.
 
Tells a spy to call through to the actual implemenatation.
 
andReturn(value)
For setting the return value of a spy.
 
andThrow(exceptionMsg)
For throwing an exception when a spy is called.
 
plan()
The acutal function this spy stubs.
 
Resets all of a spy's the tracking variables so that it can be used again.
Class Detail
jasmine.Spy(name)
Jasmine Spies are test doubles that can act as stubs, spies, fakes or when used in an expecation, mocks. Spies should be created in test setup, before expectations. They can then be checked, using the standard Jasmine expectation syntax. Spies can be checked if they were called or not and what the calling params were. A Spy has the following mehtod: wasCalled, callCount, mostRecentCall, and argsForCall (see docs) Spies are torn down at the end of every spec. Note: Do not call new jasmine.Spy() directly - a spy must be created using spyOn, jasmine.createSpy or jasmine.createSpyObj.
// a stub
var myStub = jasmine.createSpy('myStub');  // can be used anywhere

// spy example
var foo = {
  not: function(bool) { return !bool; }
}

// actual foo.not will not be called, execution stops
spyOn(foo, 'not');

 // foo.not spied upon, execution will continue to implementation
spyOn(foo, 'not').andCallThrough();

// fake example
var foo = {
  not: function(bool) { return !bool; }
}

// foo.not(val) will return val
spyOn(foo, 'not').andCallFake(function(value) {return value;});

// mock example
foo.not(7 == 7);
expect(foo.not).wasCalled();
expect(foo.not).wasCalledWith(true);
Parameters:
{String} name
See:
spyOn, jasmine.createSpy, jasmine.createSpyObj
Field Detail
argsForCall
Holds arguments for each call to the spy, indexed by call count
var mySpy = jasmine.createSpy('foo');
mySpy(1, 2);
mySpy(7, 8);
mySpy.mostRecentCall.args = [7, 8];
mySpy.argsForCall[0] = [1, 2];
mySpy.argsForCall[1] = [7, 8];

identity
The name of the spy, if provided.

isSpy
Is this Object a spy?

mostRecentCall
Tracking of the most recent call to the spy.
var mySpy = jasmine.createSpy('foo');
mySpy(1, 2);
mySpy.mostRecentCall.args = [1, 2];
Method Detail
andCallFake(fakeFunc)
Calls an alternate implementation when a spy is called.
var baz = function() {
  // do some stuff, return something
}
// defining a spy from scratch: foo() calls the function baz
var foo = jasmine.createSpy('spy on foo').andCall(baz);

// defining a spy on an existing property: foo.bar() calls an anonymnous function
spyOn(foo, 'bar').andCall(function() { return 'baz';} );
Parameters:
{Function} fakeFunc

andCallThrough()
Tells a spy to call through to the actual implemenatation.
var foo = {
  bar: function() { // do some stuff }
}

// defining a spy on an existing property: foo.bar
spyOn(foo, 'bar').andCallThrough();

andReturn(value)
For setting the return value of a spy.
// defining a spy from scratch: foo() returns 'baz'
var foo = jasmine.createSpy('spy on foo').andReturn('baz');

// defining a spy on an existing property: foo.bar() returns 'baz'
spyOn(foo, 'bar').andReturn('baz');
Parameters:
{Object} value

andThrow(exceptionMsg)
For throwing an exception when a spy is called.
// defining a spy from scratch: foo() throws an exception w/ message 'ouch'
var foo = jasmine.createSpy('spy on foo').andThrow('baz');

// defining a spy on an existing property: foo.bar() throws an exception w/ message 'ouch'
spyOn(foo, 'bar').andThrow('baz');
Parameters:
{String} exceptionMsg

plan()
The acutal function this spy stubs.

reset()
Resets all of a spy's the tracking variables so that it can be used again.
spyOn(foo, 'bar');

foo.bar();

expect(foo.bar.callCount).toEqual(1);

foo.bar.reset();

expect(foo.bar.callCount).toEqual(0);

Documentation generated by JsDoc Toolkit 2.1.0 on Wed Sep 02 2009 07:24:38 GMT-0700 (PDT)