有人可以提供使用 Test::MockObject
中的 set_bound()
方法的示例吗?模块?
set_bound()
方法可以返回 Test::MockObject
的实例吗? (或任何其他与此相关的对象)
最佳答案
*“set_bound 方法可以返回 Test::MockObject(或任何其他与此相关的对象)的实例”*
是的。来自 source :
sub set_bound {
# ...
return unless exists $bindings{reftype( $ref )};
$self->mock( $name, $bindings{reftype( $ref )} );
} # So this returns either undef, or result of mock() call
sub mock {
#...
$self;
} # So this CAN return an instance of Test::MockObject
*有人可以提供一个使用 Test::MockObject 模块中的 set_bound 方法的示例吗?*
my $value = 'X';
$mock->set_bound( 'next_value', \$value );
is( $mock->next_value, 'X' );
$var = 'Y';
is( $mock->next_value, 'Y' ); # Method result changed to new value of the variable
为什么要使用它? POD 指出“这通常比替换模拟方法更方便”。我想啤酒持有者眼中的“更方便”,但这确实是一个很好的捷径。
关于Perl 在 Test::MockObject 中使用 set_bound,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9268717/