On Github Gleeble / mcjug-jasmine
Created by Adam Swift / @Gleeble
Jasmine is a behavior-driven development framework for testing JavaScript code. It does not depend on any other JavaScript frameworks. It does not require a DOM. And it has a clean, obvious syntax so that you can easily write tests.
“You will love Jasmine. You will want to marry Jasmine. - Jim Weir”describe(description, suiteFunction)
beforeEach(setupFunction)
it(description, testFunction)
afterEach(tearDownFunction)
xdescribe() | xit()beforeEach and afterEach act as the setup and tearDown methods for the tests. They will be run progressively down the describe blocks for each set of specs. The describe() element defines a set of tests and allows for describe blocks to be nested to keep logical functionalities together. The it() function defines the test cases. The describe() and it() functions take in a descriptive string along with a function containing the functionality for the test. You can ignore tests by prefixing the suite or test functions for `x`. Jasmine 2.0 adds additional changes to provide for asynchronous testing, passing in a done function as a parameter to the beforeEach, it, and afterEach functions. Additionally, the xit and xdescribe no longer just ignore tests but they are marked as pending in the test results.
expect(variable).toBe(expectedVariable);
.toMatch(), .toBeTruthy(), .toBeFalsy(), .toContain(), .toBeLessThan(), .toBeGreaterThan(), .toBeCloseTo() .toThrow(), jasmine.any()
.toMatch() Matches a string against a regular expression. .toContain() checks against an array for an object. .toBeCloseTo() Checks against a # to see if the result is close to it. jasmine.any() can be used to check against any object types. ex. jasmine.any(Function), jasmine.any(Number), jasmine.any(Object). this cannot be used with .toBe() but does work with .toEqual() Jasmine 2.0 also adds the jasmine.objectContaining() to check that an object contains certain values.beforeEach(function() { this.addMatchers({ toBeLessThan: function(expected) { return this.actual < expected; } }); });
expect(lowNum).toBeLessThan(5);
spyOn(object, "functionName")
jasmine.createSpy("spyName")
jasmine.createSpyObj("spyName",["functionNames"])
.andReturn(val)
.andCallFake(function(){})
.andCallThrough()
.andThrow(err)In Jasmine 2.0 the syntax for these functions has changed. and.returnValue() and.callFake() and.callThrough() and.throwError() and.stub()
.toHaveBeenCalled()
.toHaveBeenCalledWith(arguments)
spy.calls[] spy.calls[0].args[] spy.mostRecentCall spy.callCountJasmine 2.0 adds a whole bunch of new utility functions to better access the calls and arguments in the spy as well as reset the spy's call information.
spyOn($, 'ajax).andCallFake(function(url, settings){ var deferred = $.Deferred(); deferred.resolve(); //or deferred.reject() return deferred.promise(); };
var timerCallback; beforeEach(function() { timerCallback = jasmine.createSpy('timerCallback'); jasmine.Clock.useMock(); }); it("causes a timeout to be called synchronously", function() { setTimeout(function() { timerCallback(); }, 100); expect(timerCallback).not.toHaveBeenCalled(); jasmine.Clock.tick(101); expect(timerCallback).toHaveBeenCalled(); });Jasmine 2.0 has additional configuration for this.