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.
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);
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");