3

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.

simbabque
  • 53,749
  • 8
  • 73
  • 136

2 Answers2

3
  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__.

ikegami
  • 367,544
  • 15
  • 269
  • 518
1

Put it in the t directory, together with the test executables.

daxim
  • 39,270
  • 4
  • 65
  • 132