【问题标题】:Perl using set_bound in Test::MockObjectPerl 在 Test::MockObject 中使用 set_bound
【发布时间】:2012-02-13 22:02:22
【问题描述】:

有人可以提供一个使用Test::MockObject模块中的set_bound()方法的例子吗?

set_bound() 方法能否返回 Test::MockObject 的实例(或任何其他对象)

【问题讨论】:

    标签: perl unit-testing testing tdd


    【解决方案1】:

    *“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 声明“这通常比替换模拟方法更方便”。我猜“handier”在啤酒持有者的眼中,但它确实是一个不错的捷径。

    【讨论】:

    • 你是绝对正确的 - 我正在替换模拟方法,但感谢你的例子! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-24
    • 2011-07-17
    • 1970-01-01
    相关资源
    最近更新 更多