Class: SpyStrategy

jasmine. SpyStrategy

new SpyStrategy()

Methods

callFake(fn)

Calls an alternate implementation when a spy is called.
Parameters:
Name Type Description
fn function
Example
foo.bar = function() {
    // do some stuff, return something
};

// defining a spy from scratch: foo() calls the function baz
var foo = jasmine.createSpy('spy on foo').and.callFake(baz);

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

callThrough()

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

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

returnValue(value)

For setting the return value of a spy.
Parameters:
Name Type Description
value Object
Example
// defining a spy from scratch: foo() returns 'baz'
var foo = jasmine.createSpy('spy on foo').and.returnValue('baz');

// defining a spy on an existing property: foo.bar() returns 'baz'
spyOn(foo, 'bar').and.returnValue('baz');

stub()

Does nothing when a spy is called. This is the default behavior of spies created by spyOn.
Example
var foo = {
    bar: function() {
        // do some stuff
    }
}

// defining a spy on an existing property: foo.bar
spyOn(foo, 'bar').and.callThrough();
foo.bar(); // calls original

foo.bar.and.stub();
foo.bar(); // noop

throwError(something)

For throwing an exception when a spy is called.
Parameters:
Name Type Description
something String
Example
// 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').and.throwError('baz');