perlunit-testingstorable

Where do I put objects frozen with Storable to use them as mock input in a unit test in Perl?


I am writing a unit test where I need to mock a function that is returning a Class::Std::Storable object. There is no normal way to serialize these using Data::Dumper and such. Instead, I can do it as follows:

use Storable;
my $serialized = Storable::freeze($object);
#store to a file, database, or wherever, and retrieve later.
my $clone = Storable::thaw($serialized);

So in my unit test, I will need to mock the function to return that stored object, maybe like this:

{
  local *foo = sub { return Storable::thaw($serialized) };
  is(call_to_something_that_calls_foo('input'), $result_of_operation_on_object);
}

That much is pretty clear. What's causing me pain is how to keep that serialized object. It very much looks like binary, so I can't just put it in the __DATA__ section like I would with SQL for a temporary in-memory sqlite db or some other data that might get put into objects. I could put it into a file and store that with my test, but is that a good idea?

So where do I put that frozen serialized object?


So Google may index this question for the future: This is in fact about SOAP::WSDL and Class::Std::Fast::Storable.


Solution

    1. Put it in t/foo.t.data and use __FILE__ . '.data' as the file name.

    2. base64-encode the data and place it after __DATA__.