对模块进行单元测试时如何模拟导入的子例程 [英] How to mock an imported subroutine when unit-testing a module
问题描述
考虑一个模块,它导出一个连接到 Internet 并返回结果的子程序:
单元模块A;子下载是导出{"result from internet" # 显然不是实际的实现.}
另一个导入并调用该子程序的模块:
使用A;# 导入和下载到这个词法范围单元模块B;子做某事是出口{下载().uc ~ "!!"# 做一些涉及调用和下载的事情}
现在我想为模块 B
编写单元测试.
但我不希望测试实际连接到 Internet;我希望他们使用由我的测试脚本控制的子程序 download
的模拟版本:
使用测试;方案二;使用 B;我的 $mock-result;我的 &mock-download = ->{ $mock-result }# ...这是安装 &mock-download 的魔术代码# as &download 在 B 的词法范围内...$mock-result = "假结果";is do-something(), "FAKE RESULT!!", "do-something works - 1";$mock-result = "foobar";is do-something(), "FOOBAR!!", "do-something works - 2";
问题是缺少覆盖子download
...
在 Perl 5 中,我认为使用 glob 赋值可以很容易地实现,或者在 Sub::Override 或 Test::MockModule.>
但是在 Perl 6 中,模块 B
的词法作用域在编译完成后关闭,因此在测试脚本运行时不能再修改(如果我错误的).所以这种方法似乎不太可能.
那么,如何在 Perl 6 中解决这个任务?
IE.如何为 B::do-something
编写单元测试,而不让它调用真正的 A::download
?
最简单的方法可能是使用 wrap
,它在 https://docs.perl6.org/language/functions#Routines 但先决条件是 use soft;
pragma这可以防止内联.您需要在模块 A 中 use soft;
:
单元模块A;使用软;子下载是导出{来自互联网的结果";}
模块 B:
单元模块B;用一个;子做某事是出口{下载.uc ~ "!!";}
和测试脚本:
使用测试;用一个;使用 B;&download.wrap({"模拟结果";});正在做某事,模拟结果!!",模拟'使用'd sub";# ok 1 - 模拟一个 'use'd sub
Consider a module which exports a subroutine that connects to the Internet and returns a result:
unit module A;
sub download is export {
"result from internet" # Not the actual implementation, obviously.
}
And another module which imports and calls that subroutine:
use A; # imports &download into this lexical scope
unit module B;
sub do-something is export {
download().uc ~ "!!" # Does something which involves calling &download
}
Now I'd like to write unit tests for module B
.
But I don't want the tests to actually connect to the Internet; I'd like them to use a mock version of subroutine download
that is controlled by my test script:
use Test;
plan 2;
use B;
my $mock-result;
my &mock-download = -> { $mock-result }
# ...Here goes magic code that installs &mock-download
# as &download in B's lexical scope...
$mock-result = "fake result";
is do-something(), "FAKE RESULT!!", "do-something works - 1";
$mock-result = "foobar";
is do-something(), "FOOBAR!!", "do-something works - 2";
The problem is the missing magic code to override sub download
...
In Perl 5, I think this could be achieved pretty easily using glob assignment, or even nicer with the help of Sub::Override or Test::MockModule.
But in Perl 6, the lexical scope of module B
closes when it has finished compiling, and can thus no longer be modified by the time the test script is run (correct me if I'm wrong). So this approach doesn't seem possible.
How would one solve this task in Perl 6, then?
I.e. how would one write unit tests for B::do-something
, without letting it call the real A::download
?
The simplest approach might be to use wrap
which is described in https://docs.perl6.org/language/functions#Routines but a prerequisite for that is the use soft;
pragma which prevents inlining. You'd need to use soft;
in module A:
unit module A;
use soft;
sub download is export {
"result from internet";
}
Module B:
unit module B;
use A;
sub do-something is export {
download.uc ~ "!!";
}
And the test script:
use Test;
use A;
use B;
&download.wrap({
"mock result";
});
is do-something, "MOCK RESULT!!", "mock a 'use'd sub";
# ok 1 - mock a 'use'd sub
这篇关于对模块进行单元测试时如何模拟导入的子例程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!