Merge branch 'toHaveBeenCalledBefore' of https://github.com/foretagsplatsen/jasmine into foretagsplatsen-toHaveBeenCalledBefore

- Merges #1242 from @DamienCassou
This commit is contained in:
Gregg Van Hove
2017-03-10 09:54:23 -08:00
5 changed files with 224 additions and 0 deletions

View File

@@ -1,5 +1,13 @@
getJasmineRequireObj().Spy = function (j$) {
var nextOrder = (function() {
var order = 0;
return function() {
return order++;
};
})();
function Spy(name, originalFn) {
var args = buildArgs(),
/*`eval` is the only option to preserve both this and context:
@@ -21,6 +29,7 @@ getJasmineRequireObj().Spy = function (j$) {
spy = function () {
var callData = {
object: this,
invocationOrder: nextOrder(),
args: Array.prototype.slice.apply(arguments)
};

View File

@@ -15,6 +15,7 @@ getJasmineRequireObj().requireMatchers = function(jRequire, j$) {
'toContain',
'toEqual',
'toHaveBeenCalled',
'toHaveBeenCalledBefore',
'toHaveBeenCalledWith',
'toHaveBeenCalledTimes',
'toMatch',

View File

@@ -0,0 +1,52 @@
getJasmineRequireObj().toHaveBeenCalledBefore = function(j$) {
var getErrorMsg = j$.formatErrorMsg('<toHaveBeenCalledBefore>', 'expect(<spyObj>).toHaveBeenCalledBefore(<spyObj>)');
function toHaveBeenCalledBefore() {
return {
compare: function(firstSpy, latterSpy) {
if (!j$.isSpy(firstSpy)) {
throw new Error(getErrorMsg('Expected a spy, but got ' + j$.pp(firstSpy) + '.'));
}
if (!j$.isSpy(latterSpy)) {
throw new Error(getErrorMsg('Expected a spy, but got ' + j$.pp(latterSpy) + '.'));
}
var result = { pass: false };
if (!firstSpy.calls.count()) {
result.message = 'Expected spy ' + firstSpy.and.identity() + ' to have been called.';
return result;
}
if (!latterSpy.calls.count()) {
result.message = 'Expected spy ' + latterSpy.and.identity() + ' to have been called.';
return result;
}
var latest1stSpyCall = firstSpy.calls.mostRecent().invocationOrder;
var first2ndSpyCall = latterSpy.calls.first().invocationOrder;
result.pass = latest1stSpyCall < first2ndSpyCall;
if (result.pass) {
result.message = 'Expected spy ' + firstSpy.and.identity() + ' to not have been called before spy ' + latterSpy.and.identity() + ', but it was';
} else {
var first1stSpyCall = firstSpy.calls.first().invocationOrder;
var latest2ndSpyCall = latterSpy.calls.mostRecent().invocationOrder;
if(first1stSpyCall < first2ndSpyCall) {
result.message = 'Expected latest call to spy ' + firstSpy.and.identity() + ' to have been called before first call to spy ' + latterSpy.and.identity() + ' (no interleaved calls)';
} else if (latest2ndSpyCall > latest1stSpyCall) {
result.message = 'Expected first call to spy ' + latterSpy.and.identity() + ' to have been called after latest call to spy ' + firstSpy.and.identity() + ' (no interleaved calls)';
} else {
result.message = 'Expected spy ' + firstSpy.and.identity() + ' to have been called before spy ' + latterSpy.and.identity();
}
}
return result;
}
};
}
return toHaveBeenCalledBefore;
};