Create a spy
// "bare" spy
var spy = jasmine.createSpy('spyName');
// Mock object of spies: spy.next(), spy.current(), etc
var spy = jasmine.createSpyObj('spy', ['next', 'current', 'count', 'index']);
// Most common use: replace object method with spy
var spy = spyOn(myObject, 'methodName');
spyOn(myObject, 'methodName');
// Add spy but also call original method
var spy = spyOn(myObject, 'methodName').and.callThrough();
// Spy on and replace a method
var spy = spyOn(myObject, 'methodName').and.callFake(function() {
return 'blergh';
});
Spy expectations
expect(spy).toHaveBeenCalled();
expect(spy).toHaveBeenCalledWith(jasmine.any(Object), 'foo');
expect(spy.calls.mostRecent().args[1]).toEqual('foo');
expect(spy.calls.first().args[1]).toEqual('qux');
expect(spy.calls.count()).toEqual(2);
expect(true).toBe(true);
expect(false).not.toBe(true);
expect([1,2,3].length).toEqual(3);
expect(undefined).not.toBeDefined();
expect(null).toBeNull();
expect([1,2,3]).toEqual(jasmine.any(Array));
// jasmine.any takes any constructor as its argument, allowing matching types
expect(function(cb) { cb(); }).toEqual(jasmine.any(Function));
expect(results).toEqual(jasmine.objectContaining({'foo': 'bar'}));
expect([1,2,3]).toContain(2);
expect(true).toBeTruthy(); // casts to boolean
expect(false).toBeFalsy();
expect(Math.PI).toBeGreaterThan(3);
expect(Math.PI).toBeLessThan(3.2);
expect(Math.PI).toBeCloseTo(3.14, 2); // second param is precision
expect({} + {}).toBeNan();
expect('hullo this is foo').toMatch(/foo$/);
expect('hullo this is foo').toMatch('hullo');
jasmine-jquery adds a lot of excellent matchers. As of this writing, all 99designs apps use jasmine-jquery
Setup
beforeEach(function() {
jasmine.Ajax.install();
});
afterEach(function() {
jasmine.Ajax.uninstall();
});
Inspecting and responding to request
var request = jasmine.Ajax.requests.mostRecent();
expect(request.url).toEqual('http://example.com/customers?query=Frank');
expect(request.method).toEqual('GET');
request.respondWith({'foo' : 'bar'});
Setup
beforeEach(function() {
jasmine.clock().install();
});
afterEach(function() {
jasmine.clock().uninstall();
});
Manipulating time
var timeSpy = jasmine.createSpy('timeSpy');
setTimeout(timeSpy, 200);
expect(timeSpy).not.toHaveBeenCalled();
jasmine.clock().tick(201);
expect(timeSpy).toHaveBeenCalled();
describe('description', function(){});
ddescribe('description', function(){}); // will only run this
xdescribe('description', function(){}); // won't run this
it('description', function(){});
iit('description', function(){}); // will only run this
xit('description', function(){}); // won't run this
// it() with only one argument is a pending spec.
// Use this to outline your design
it('puts the lotion in the basket');
@jamos and myself have just been dealing with testing some async functions. It's handy to know that a
done
callback is passed intoit
,beforeEach
andafterEach
which can optionally be executed to confirm that all processing has been completed.http://jasmine.github.io/2.0/introduction.html#section-Asynchronous_Support