diff --git a/spec/core/matchers/toHaveBeenCalledBeforeSpec.js b/spec/core/matchers/toHaveBeenCalledBeforeSpec.js index 2a3b602d..256ed1d2 100644 --- a/spec/core/matchers/toHaveBeenCalledBeforeSpec.js +++ b/spec/core/matchers/toHaveBeenCalledBeforeSpec.js @@ -50,10 +50,40 @@ describe("toHaveBeenCalledBefore", function() { result = matcher.compare(firstSpy, secondSpy); expect(result.pass).toBe(false); - expect(result.message).toEqual('Expected first spy to have been called before second spy'); + expect(result.message).toEqual('Expected spy first spy to have been called before spy second spy'); }); - it("passes when first spy is called before second spy", function() { + it("fails when the actual is called before and after the expected", function() { + var matcher = jasmineUnderTest.matchers.toHaveBeenCalledBefore(), + firstSpy = jasmineUnderTest.createSpy('first spy'), + secondSpy = jasmineUnderTest.createSpy('second spy'), + result; + + firstSpy(); + secondSpy(); + firstSpy(); + + result = matcher.compare(firstSpy, secondSpy); + expect(result.pass).toBe(false); + expect(result.message).toEqual('Expected latest call to spy first spy to have been called before first call to spy second spy (no interleaved calls)'); + }); + + it("fails when the expected is called before and after the actual", function() { + var matcher = jasmineUnderTest.matchers.toHaveBeenCalledBefore(), + firstSpy = jasmineUnderTest.createSpy('first spy'), + secondSpy = jasmineUnderTest.createSpy('second spy'), + result; + + secondSpy(); + firstSpy(); + secondSpy(); + + result = matcher.compare(firstSpy, secondSpy); + expect(result.pass).toBe(false); + expect(result.message).toEqual('Expected first call to spy second spy to have been called after latest call to spy first spy (no interleaved calls)'); + }); + + it("passes when the actual is called before the expected", function() { var matcher = jasmineUnderTest.matchers.toHaveBeenCalledBefore(), firstSpy = jasmineUnderTest.createSpy('first spy'), secondSpy = jasmineUnderTest.createSpy('second spy'), @@ -64,6 +94,6 @@ describe("toHaveBeenCalledBefore", function() { result = matcher.compare(firstSpy, secondSpy); expect(result.pass).toBe(true); - expect(result.message).toEqual('Expected first spy to not have been called before second spy, but it was'); + expect(result.message).toEqual('Expected spy first spy to not have been called before spy second spy, but it was'); }); }); diff --git a/src/core/matchers/toHaveBeenCalledBefore.js b/src/core/matchers/toHaveBeenCalledBefore.js index 57634162..b54973f1 100644 --- a/src/core/matchers/toHaveBeenCalledBefore.js +++ b/src/core/matchers/toHaveBeenCalledBefore.js @@ -23,12 +23,24 @@ getJasmineRequireObj().toHaveBeenCalledBefore = function(j$) { return result; } - result.pass = firstSpy.calls.mostRecent().invocationOrder < latterSpy.calls.mostRecent().invocationOrder; + var latest1stSpyCall = firstSpy.calls.mostRecent().invocationOrder; + var first2ndSpyCall = latterSpy.calls.first().invocationOrder; + + result.pass = latest1stSpyCall < first2ndSpyCall; if (result.pass) { - result.message = 'Expected ' + firstSpy.and.identity() + ' to not have been called before ' + latterSpy.and.identity() + ', but it was'; + result.message = 'Expected spy ' + firstSpy.and.identity() + ' to not have been called before spy ' + latterSpy.and.identity() + ', but it was'; } else { - result.message = 'Expected ' + firstSpy.and.identity() + ' to have been called before ' + latterSpy.and.identity(); + 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;