Last active
June 20, 2019 13:36
-
-
Save mplatts/3850920 to your computer and use it in GitHub Desktop.
Backbone Testing Cheatsheet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Libraries used: | |
# jasmine | |
# jasmine-jquery | |
# jasmine-sinon | |
# sinon | |
##### Basics Overview ##### | |
describe "APP_NAME.Views.VIEW_FOLDER.VIEW_NAME ", -> | |
beforeEach -> | |
# Stuff run before each test | |
afterEach -> | |
# Stuff run after each test | |
describe "render", -> | |
it "should be visible", -> | |
expect($(@app.el)).toContain("#flash-message") # with jquery jasmine matchers | |
expect($(".pickImage", @view.el)).toExist() # without jquery jasmine matchers | |
##### Frequent Tests ##### | |
# Make sure a function is called: | |
beforeEach( -> | |
collection = new SomethingCollection() | |
spyOn(collection, "fetch") | |
view = new SomethingView({collection: collection}) | |
) | |
it("should fetch the collection", function() { | |
expect(collection.fetch).toHaveBeenCalled() | |
) | |
##### Jasmine Matchers (using jQuery) ##### | |
expect($(@app.el)).toContain("#flash-message") | |
expect($("div.item", @view.el).length).toEqual(1) | |
expect(iframe.find("style#inlineBlurbCss")).not.toExist() | |
expect(x).toEqual(y) | |
expect(x).toMatch(pattern) | |
expect(x).toBeDefined() | |
expect(x).toBeNull() | |
expect(x).toBeTruthy() | |
expect(x).toBeFalsy() | |
expect(x).toContain(y) | |
expect(x).not.toEqual(y) | |
##### Jasmine Spying ##### | |
spyOn(obj, 'method') | |
spyOn(obj, 'method').andReturn('Pow!') | |
expect(obj.method).toHaveBeenCalled() | |
expect(obj.method).toHaveBeenCalledWith('foo', 'bar') | |
obj.method.callCount | |
obj.method.mostRecentCall.args | |
##### Sinon ##### | |
@stub = sinon.stub(@view, "renderHeaderItems") | |
expect(@stub).toHaveBeenCalled() # needs jasmine-sinon.js | |
@view.renderHeaderItems.restore() | |
# Server Manual Respond # | |
server = sinon.fakeServer.create() | |
server.respondWith "GET", "resources.json", [200, "Content-Type": "application/json", (JSON.stringify(@resourcesFixture))] | |
$("a.pickImage", @view.el).trigger("click") | |
server.respond() | |
server.restore() | |
@xhr = sinon.useFakeXMLHttpRequest(); | |
@requests = [] | |
@xhr.onCreate = (xhr) => @requests.push(xhr) | |
# Server Auto Response # | |
$.ajaxSetup(async: false) | |
@fixture = JSON.parse(@readFixture("portal_loading/sales_listings.json")) | |
@collection = new Backbone.Collection(ids: [1,2,3], search_title: "foo") | |
@server = sinon.fakeServer.create() | |
response = JSON.stringify(@fixture) | |
@server.respondWith "GET", /.*sales_listings.*json/, [200, "Content-Type": "application/json", response] | |
@server.respondWith "POST", /.*/, [200, "Content-Type": "application/json", ""] | |
@server.autoRespond = true | |
@server.autoRespondAfter = 0 | |
@collection.fetch() | |
##### Helpers ##### | |
# mock all reqeusts, nothing should hit the server. | |
@xhr = sinon.useFakeXMLHttpRequest(); | |
@requests = [] | |
@xhr.onCreate = (xhr) => @requests.push(xhr) | |
# Fixtures | |
jasmine.getFixtures().fixturesPath = "/assets/fixtures" | |
app.headers = jasmine.getFixtures().read("contact_import/headers.json") | |
##### Misc ##### | |
waitsFor( (=> $("#previewFrame", @el).contents().find("div").length), "frame to load") | |
Sinon.JS & Jasmine BDD Cheat Sheet (https://github.com/mattfysh/cheat-sinon-jasmine) | |
Spy | |
*sinon* | |
spy() | |
spy(fn) | |
spy(obj, 'fn') | |
*spy* | |
callCount | |
called | |
calledOnce | |
calledTwice | |
calledThrice | |
calledBefore(spy) | |
calledAfter(spy) | |
calledOn(obj) | |
alwaysCalledOn(obj) | |
calledWith(*) | |
alwaysCalledWith(*) | |
calledWithExactly(*) | |
alwaysCalledWithExactly(*) | |
threw() | |
threw('err') | |
threw(obj) | |
alwaysThrew() | |
alwaysThrew('err') | |
alwaysThrew(obj) | |
returned(obj) | |
alwaysReturned(obj) | |
thisValues | |
args | |
exceptions | |
returnValues | |
*spyCall --- getCall(n)* | |
calledOn(obj) | |
calledWith(*) | |
calledWithExactly(*) | |
threw() | |
threw('err') | |
threw(obj) | |
thisValue | |
args | |
exception | |
returnValue | |
* argument list | |
Stub | |
*sinon* | |
stub() | |
stub(obj, "fn") | |
stub(obj, "fn", fn | |
stub(obj) | |
*stub* | |
withArgs(*) | |
returns(obj) | |
throws() | |
throws('err') | |
throws(obj) | |
callsArg(index) | |
callsArgWith(index, *) | |
yields(*) | |
yieldsTo(property, *) | |
Mock | |
*sinon* | |
mock(obj) | |
mock() | |
*mock* | |
verify() | |
restore() | |
*expectation --- expects('fn')* | |
atLeast(n) | |
atMost(n) | |
never() | |
once() | |
twice() | |
thrice() | |
exactly(n) | |
withArgs(*) | |
withExactArgs(*) | |
on(obj) | |
verify() | |
Matchers | |
Jasmine Matchers | |
*expect(x)* | |
toEqual(y) | |
toBe(y) | |
toMatch(pattern) | |
toBeDefined() | |
toBeNull() | |
toBeTruthy() | |
toBeFalsy() | |
toContain() | |
toBeLessThan(y) | |
toBeGreaterThan(y) | |
toHaveBeenCalled(y) | |
toHaveBeenCalledWith(y) | |
*expect(fn)* | |
toThrow(e) | |
*expect(e).not* - negate criteria | |
Sinon.JS Matchers | |
*expect(spy)* | |
toHaveBeen | |
Called() | |
CalledOnce() | |
CalledTwice() | |
CalledThrice() | |
CalledBefore(spy) | |
CalledAfter(spy) | |
CalledOn(obj) | |
AlwaysCalledOn(obj) | |
CalledWith(*) | |
AlwaysCalledWith(*) | |
CalledWithExactly(*) | |
AlwaysCalledWithExactly(*) | |
toHaveReturned(obj) | |
toHaveAlwaysReturned(obj) | |
*expect(spy).not* - negate criteria |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment