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.
69 lines
2.4 KiB
JavaScript
69 lines
2.4 KiB
JavaScript
describe("ArrayContaining", function() {
|
|
it("matches any actual to an empty array", function() {
|
|
var containing = new jasmineUnderTest.ArrayContaining([]);
|
|
|
|
expect(containing.asymmetricMatch("foo")).toBe(true);
|
|
});
|
|
|
|
it("does not work when not passed an array", function() {
|
|
var containing = new jasmineUnderTest.ArrayContaining("foo");
|
|
|
|
expect(function() {
|
|
containing.asymmetricMatch([]);
|
|
}).toThrowError(/not 'foo'/);
|
|
});
|
|
|
|
it("matches when the item is in the actual", function() {
|
|
var containing = new jasmineUnderTest.ArrayContaining(["foo"]);
|
|
var matchersUtil = new jasmineUnderTest.MatchersUtil();
|
|
|
|
expect(containing.asymmetricMatch(["foo"], matchersUtil)).toBe(true);
|
|
});
|
|
|
|
it("matches when additional items are in the actual", function() {
|
|
var containing = new jasmineUnderTest.ArrayContaining(["foo"]);
|
|
var matchersUtil = new jasmineUnderTest.MatchersUtil();
|
|
|
|
expect(containing.asymmetricMatch(["foo", "bar"], matchersUtil)).toBe(true);
|
|
});
|
|
|
|
it("does not match when the item is not in the actual", function() {
|
|
var containing = new jasmineUnderTest.ArrayContaining(["foo"]);
|
|
var matchersUtil = new jasmineUnderTest.MatchersUtil();
|
|
|
|
expect(containing.asymmetricMatch(["bar"], matchersUtil)).toBe(false);
|
|
});
|
|
|
|
it("does not match when the actual is not an array", function() {
|
|
var containing = new jasmineUnderTest.ArrayContaining(["foo"]);
|
|
var matchersUtil = new jasmineUnderTest.MatchersUtil();
|
|
|
|
expect(containing.asymmetricMatch("foo", matchersUtil)).toBe(false);
|
|
});
|
|
|
|
it("jasmineToStrings itself", function() {
|
|
var sample = [],
|
|
matcher = new jasmineUnderTest.ArrayContaining(sample),
|
|
pp = jasmine.createSpy('pp').and.returnValue('sample');
|
|
|
|
expect(matcher.jasmineToString(pp)).toEqual(
|
|
'<jasmine.arrayContaining(sample)>'
|
|
);
|
|
expect(pp).toHaveBeenCalledWith(sample);
|
|
});
|
|
|
|
it("uses custom equality testers", function() {
|
|
var tester = function(a, b) {
|
|
// All "foo*" strings match each other.
|
|
if (typeof a == "string" && typeof b == "string" &&
|
|
a.substr(0, 3) == "foo" && b.substr(0, 3) == "foo") {
|
|
return true;
|
|
}
|
|
};
|
|
var containing = new jasmineUnderTest.ArrayContaining(["fooVal"]);
|
|
var matchersUtil = new jasmineUnderTest.MatchersUtil({customTesters: [tester]});
|
|
|
|
expect(containing.asymmetricMatch(["fooBar"], matchersUtil)).toBe(true);
|
|
});
|
|
});
|