Perl 在 Test::MockObject 中使用 set_bound

标签 perl unit-testing testing tdd

有人可以提供使用 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/

相关文章:

Perl "reverse comma operator"(来自《Programming Perl》一书的示例,第 4 版)

database - 我如何用 DBIx::Class 建立一个 is-a 关系模型?

php - Codeception\Util\Stub 方法::exactly 和::once 不起作用

testing - 使用 gradle 运行单个集成测试

html - 将 HTML 源渲染为图像(png、gif) - Cocoa、Applescript、Automator 一切都受欢迎

windows - 用于创建简单 Microsoft Windows GUI 的 Perl 模块?

java - JUnit 测试失败案例

unit-testing - 客户端-服务器应用程序上的 TDD

sql - 如何避免手动编写/管理 SQL

java - 在 while 循环中验证 isDisplayed() WebElement 属性