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.

4

2 回答 2

3
  1. 放入t/foo.t.data__FILE__ . '.data'用作文件名。

  2. 对数据进行 base64 编码并将其放在__DATA__.

于 2013-11-12T19:40:30.427 回答
1

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

于 2013-11-12T18:37:52.037 回答