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.
68 lines
2.2 KiB
JavaScript
68 lines
2.2 KiB
JavaScript
describe("Custom object formatters", function() {
|
|
var env;
|
|
|
|
beforeEach(function() {
|
|
env = new jasmineUnderTest.Env();
|
|
env.configure({random: false});
|
|
});
|
|
|
|
it("scopes custom object formatters to a spec", function(done) {
|
|
env.it('a spec with custom pretty-printer', function() {
|
|
env.addCustomObjectFormatter(function(obj) { return 'custom(' + obj + ')'; });
|
|
env.expect(42).toBeUndefined();
|
|
});
|
|
|
|
env.it('a spec without custom pretty-printer', function() {
|
|
env.expect(42).toBeUndefined();
|
|
});
|
|
|
|
var specResults = [];
|
|
var specDone = function(result) {
|
|
specResults.push(result);
|
|
};
|
|
var expectations = function() {
|
|
expect(specResults[0].failedExpectations[0].message).toEqual("Expected custom(42) to be undefined.");
|
|
expect(specResults[1].failedExpectations[0].message).toEqual("Expected 42 to be undefined.");
|
|
done();
|
|
};
|
|
env.addReporter({ specDone:specDone, jasmineDone: expectations});
|
|
|
|
env.execute();
|
|
});
|
|
|
|
it("scopes custom object formatters to a suite", function(done) {
|
|
env.it('a spec without custom pretty-printer', function() {
|
|
env.expect(42).toBeUndefined();
|
|
});
|
|
|
|
env.describe('with custom pretty-printer', function() {
|
|
env.beforeEach(function() {
|
|
env.addCustomObjectFormatter(function(obj) { return 'custom(' + obj + ')'; });
|
|
});
|
|
|
|
env.it('a spec', function() {
|
|
env.expect(42).toBeUndefined();
|
|
});
|
|
});
|
|
|
|
var specResults = [];
|
|
var specDone = function(result) {
|
|
specResults.push(result);
|
|
};
|
|
var expectations = function() {
|
|
expect(specResults[0].failedExpectations[0].message).toEqual("Expected 42 to be undefined.");
|
|
expect(specResults[1].failedExpectations[0].message).toEqual("Expected custom(42) to be undefined.");
|
|
done();
|
|
};
|
|
env.addReporter({ specDone:specDone, jasmineDone: expectations});
|
|
|
|
env.execute();
|
|
});
|
|
|
|
it("throws an exception if you try to add a custom object formatter outside a runable", function() {
|
|
expect(function() {
|
|
env.addCustomObjectFormatter(function() {});
|
|
}).toThrowError('Custom object formatters must be added in a before function or a spec')
|
|
});
|
|
});
|