Skip to content

Latest commit

 

History

History
60 lines (41 loc) · 2.29 KB

Writers.md

File metadata and controls

60 lines (41 loc) · 2.29 KB

Writers

Contents

Default Behaviour

By default, when Approval Tests verifies an object, it uses the StringWriter class to write out the string representation of the object.

And by default, StringWriter gives the saved file the extension .txt.

StringWriter implements the ApprovalWriter interface.

Using custom writers

Suppose that you are serialising an object that cannot easily be represented in a text file, such as an image. In this case, the built-in StringWriter is not suitable, and you will need to write and use a custom implementation of ApprovalWriter.

Here is a simple example of using a custom writer to produce an HTML file.

HtmlWriter writer("<h1>hello world</h1>", ".html");
Approvals::verify(writer);

snippet source / anchor

Using custom filename extensions

Suppose that you are serializing an object in some text format like JSON or CSV. By writing to this file extension, different tools will render it appropriately.

If all you want to do is change the file extension, there are many convenience functions to enable this, for example:

Approvals::verifyWithExtension("<h1>hello world</h1>", ".html");

snippet source / anchor


Back to User Guide