Custom object formatters allow users to customize how an object is
stringified in matcher failure messages. This can already be done by
adding a `jasmineToString` method to the objects in question. But
it's not always desirable or possible to do that, particularly when
objects of a given "type" do not inherit from a specific prototype.
For instance, suppose a web service returns a list of foos that are
deserialized from JSON, e.g.:
{ fooId: 42, /* more properties */ }
The only way to define `jasmineToString` on those is by writing code to
add it to each instance at runtime. But a custom object formatter can
recognize that the object it's looking at is a foo and format it
accordingly:
jasmine.addCustomObjectFormatter(function(obj) {
if (typeof obj.fooId !== 'number') {
return undefined;
}
return '[Foo with ID ' + obj.fooId + ']';
});
Unlike `jasmineToString`, custom object formatters are scoped to a
particular spec or suite and don't require any changes to the code
under test.
75 lines
3.1 KiB
JavaScript
75 lines
3.1 KiB
JavaScript
describe("toBe", function() {
|
|
it("passes with no message when actual === expected", function() {
|
|
var util = new jasmineUnderTest.MatchersUtil(),
|
|
matcher = jasmineUnderTest.matchers.toBe(util),
|
|
result;
|
|
|
|
result = matcher.compare(1, 1);
|
|
expect(result.pass).toBe(true);
|
|
});
|
|
|
|
it("passes with a custom message when expected is an array", function() {
|
|
var util = new jasmineUnderTest.MatchersUtil({pp: jasmineUnderTest.makePrettyPrinter()}),
|
|
matcher = jasmineUnderTest.matchers.toBe(util),
|
|
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 util = new jasmineUnderTest.MatchersUtil({pp: jasmineUnderTest.makePrettyPrinter()}),
|
|
matcher = jasmineUnderTest.matchers.toBe(util),
|
|
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 util = new jasmineUnderTest.MatchersUtil(),
|
|
matcher = jasmineUnderTest.matchers.toBe(util),
|
|
result;
|
|
|
|
result = matcher.compare(1, 2);
|
|
expect(result.pass).toBe(false);
|
|
expect(result.message).toBeUndefined();
|
|
});
|
|
|
|
it("fails with a custom message when expected is an array", function() {
|
|
var util = new jasmineUnderTest.MatchersUtil({pp: jasmineUnderTest.makePrettyPrinter()}),
|
|
matcher = jasmineUnderTest.matchers.toBe(util),
|
|
result;
|
|
|
|
result = matcher.compare([1], [1]);
|
|
expect(result.pass).toBe(false);
|
|
expect(result.message).toBe("Expected [ 1 ] to be [ 1 ]. Tip: To check for deep equality, use .toEqual() instead of .toBe().")
|
|
});
|
|
|
|
it("fails with a custom message when expected is an object", function() {
|
|
var util = new jasmineUnderTest.MatchersUtil({pp: jasmineUnderTest.makePrettyPrinter()}),
|
|
matcher = jasmineUnderTest.matchers.toBe(util),
|
|
result;
|
|
|
|
result = matcher.compare({foo: "bar"}, {foo: "bar"});
|
|
expect(result.pass).toBe(false);
|
|
expect(result.message).toBe("Expected Object({ foo: 'bar' }) to be Object({ foo: 'bar' }). Tip: To check for deep equality, use .toEqual() instead of .toBe().")
|
|
});
|
|
|
|
it("works with custom object formatters when expected is an object", function() {
|
|
var formatter = function(x) { return '<' + x.foo + '>'; },
|
|
prettyPrinter = jasmineUnderTest.makePrettyPrinter([formatter]),
|
|
util = new jasmineUnderTest.MatchersUtil({pp: prettyPrinter}),
|
|
matcher = jasmineUnderTest.matchers.toBe(util),
|
|
result;
|
|
|
|
result = matcher.compare({foo: "bar"}, {foo: "bar"});
|
|
expect(result.pass).toBe(false);
|
|
expect(result.message).toBe("Expected <bar> to be <bar>. Tip: To check for deep equality, use .toEqual() instead of .toBe().")
|
|
});
|
|
});
|