简体   繁体   English

在Perl的单元测试中,我将在Storable中冻结的对象放在何处使用它们作为模拟输入?

[英]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. 我正在编写一个单元测试,我需要模拟一个返回Class :: Std :: Storable对象的函数。 There is no normal way to serialize these using Data::Dumper and such. 使用Data :: Dumper等没有正常的方法来序列化这些。 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. 它看起来像二进制,所以我不能像在SQL中那样将它放在__DATA__部分中,用于临时内存中的sqlite数据库或其他可能被放入对象的数据。 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 . 所以谷歌可能会为未来索引这个问题:这实际上是关于SOAP :: WSDLClass :: Std :: Fast :: Storable

  1. Put it in t/foo.t.data and use __FILE__ . '.data' 把它放在t/foo.t.data并使用__FILE__ . '.data' __FILE__ . '.data' as the file name. __FILE__ . '.data'作为文件名。

  2. base64-encode the data and place it after __DATA__ . base64-对数据进行编码并将其放在__DATA__

将它与测试可执行文件一起放在t目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM