diff --git a/spec/core/matchers/toBeSpec.js b/spec/core/matchers/toBeSpec.js index d4179906..e065af57 100644 --- a/spec/core/matchers/toBeSpec.js +++ b/spec/core/matchers/toBeSpec.js @@ -1,14 +1,34 @@ describe("toBe", function() { - it("passes when actual === expected", function() { - var matcher = jasmineUnderTest.matchers.toBe(), + it("passes with no message when actual === expected", function() { + var matcher = jasmineUnderTest.matchers.toBe(jasmineUnderTest.matchersUtil), result; result = matcher.compare(1, 1); expect(result.pass).toBe(true); }); + it("passes with a custom message when expected is an array", function() { + var matcher = jasmineUnderTest.matchers.toBe(jasmineUnderTest.matchersUtil), + result, + array = [1]; + + result = matcher.compare(array, array); + expect(result.pass).toBe(true); + expect(result.message).toBe("Expected [ 1 ] not to be [ 1 ]. Tip: To check for deep equality, use .toEqual() instead of .toBe().") + }); + + it("passes with a custom message when expected is an object", function() { + var matcher = jasmineUnderTest.matchers.toBe(jasmineUnderTest.matchersUtil), + result, + obj = {foo: "bar"}; + + result = matcher.compare(obj, obj); + expect(result.pass).toBe(true); + expect(result.message).toBe("Expected Object({ foo: 'bar' }) not to be Object({ foo: 'bar' }). Tip: To check for deep equality, use .toEqual() instead of .toBe().") + }); + it("fails with no message when actual !== expected", function() { - var matcher = jasmineUnderTest.matchers.toBe(), + var matcher = jasmineUnderTest.matchers.toBe(jasmineUnderTest.matchersUtil), result; result = matcher.compare(1, 2); @@ -17,8 +37,8 @@ describe("toBe", function() { }); it("fails with a custom message when expected is an array", function() { - var matcher = jasmineUnderTest.matchers.toBe(), - result; + var matcher = jasmineUnderTest.matchers.toBe(jasmineUnderTest.matchersUtil), + result; result = matcher.compare([1], [1]); expect(result.pass).toBe(false); @@ -26,8 +46,8 @@ describe("toBe", function() { }); it("fails with a custom message when expected is an object", function() { - var matcher = jasmineUnderTest.matchers.toBe(), - result; + var matcher = jasmineUnderTest.matchers.toBe(jasmineUnderTest.matchersUtil), + result; result = matcher.compare({foo: "bar"}, {foo: "bar"}); expect(result.pass).toBe(false); diff --git a/src/core/matchers/toBe.js b/src/core/matchers/toBe.js index d7b9e593..96b236a7 100644 --- a/src/core/matchers/toBe.js +++ b/src/core/matchers/toBe.js @@ -1,4 +1,4 @@ -getJasmineRequireObj().toBe = function($j) { +getJasmineRequireObj().toBe = function(j$) { /** * {@link expect} the actual value to be `===` to the expected value. * @function @@ -7,16 +7,20 @@ getJasmineRequireObj().toBe = function($j) { * @example * expect(thing).toBe(realThing); */ - function toBe() { - + function toBe(util) { + var tip = ' Tip: To check for deep equality, use .toEqual() instead of .toBe().'; + return { compare: function(actual, expected) { - var customMessage = 'Expected ' + $j.pp(expected) + ' to be ' + $j.pp(actual) + '. Tip: To check for deep equality, use .toEqual() instead of .toBe().'; - - return { + var result = { pass: actual === expected, - message: typeof expected === 'object' ? customMessage : undefined, }; + + if (typeof expected === 'object') { + result.message = util.buildFailureMessage('toBe', result.pass, actual, expected) + tip; + } + + return result; } }; }