Created
May 28, 2015 19:56
-
-
Save shamansir/d8813617cd82ef652f72 to your computer and use it in GitHub Desktop.
Jasmine Custom Matchers
This file contains 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
// allows to check if the spy was called with specified arguments in exact order, | |
// like: | |
// ``` | |
// var spy = createSpy('foo'); | |
// spy('a', { prop: 'foo' }); | |
// spy('b', { prop: 'bar' }); | |
// spy('c', { prop: 'baz' }); | |
// spy('d', { prop: 'fuz' }); | |
// expect(spy).toHaveBeenCalledInOrder() {[ | |
// [ 'a', jasmine.objectContaining({ prop: 'foo' }) ], | |
// [ 'c', jasmine.anything() ], | |
// [ jasmine.anything(), jasmine.objectContaining({ prop: 'fuz' }) ] | |
// ]}; | |
// ``` | |
jasmine.addMatchers({ | |
toHaveBeenCalledInOrder: function(util, customEqualityTesters) { | |
return { | |
compare: function(actual, expected) { | |
var result = { pass: false }; | |
var actual_count = actual.calls.count(); | |
if (expected.length > actual_count) { | |
result.message = 'Expected spy ' + actual.and.identity() + | |
' to have been called at least ' + expected.length + ' times,' + | |
' but it was called only ' + actual.calls.count() + ' times'; | |
return result; | |
} | |
var expected_clone = [].concat(expected); | |
for (var i = 0, ei = 0; i < actual_count; i++) { | |
if (util.equals(actual.calls.argsFor(i), expected[ei], customEqualityTesters)) { | |
expected_clone.pop(); ei++; | |
} | |
} | |
if (expected_clone.length > 0) { | |
result.message = 'Expected spy ' + actual.and.identity() + | |
' to have been called with ' + expected_clone.pop() + ', but it was not.'; | |
return result; | |
} | |
result.pass = true; | |
return result; | |
} | |
} | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment