更改 $*DISTRO 值以进行测试 [英] Changing $*DISTRO values for testing

查看:52
本文介绍了更改 $*DISTRO 值以进行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要测试一个功能,包括

I need to test a feature that includes this line:

if $translate-nl && $*DISTRO.is-win

我曾尝试为 $*DISTRO 重新分配一个值,

I have tried to reassign a value to $*DISTRO,

$*DISTRO='Windows 10';

但它说:

Cannot modify an immutable Distro (debian (9.stretch))␤

$*DISTRO 是一个动态变量,它没有被修改是有道理的.也就是说,有没有其他方法可以测试代码(当然,除了去 Windows)

$*DISTRO is a dynamic variable, and it makes sense that it's not modified. That said, is there any other way that code can be tested (other than going to Windows, of course)

推荐答案

详细说明 raiph 的答案:$*DISTRO 中的 * 将其标记为动态变量.您可以在任何范围内重新声明它,从那里调用的代码将看到重新声明的值:

To elaborate on raiph's answer: the * in $*DISTRO marks it as a dynamic variable. You can re-declare it any scope, and code called from there will see the redeclared value:

{
    my $*DISTRO = ...;
    # coded called from here sees the redeclared value
}
# code called from here sees the original value

现在,问题仍然存在,你用什么来代替这些讨厌的...?

Now, the question remains, what do you put in place of these pesky ...?

在最简单的情况下,一个只包含被测代码所需的模拟:

In the simplest case, a mock that just has whatever the code under test needs:

{
    my class Distro { has $.is-win }
    my $*DISTRO = Distro.new( :is-win );
    # call your test code here
}

如果代码需要在 Distro 中添加更多属性,只需将它们添加到模拟 Distro 类中即可.

If the code needs more attributes in Distro, just add them to the mock Distro class.

如果代码需要一个真正的* Distro 对象,出于某种原因,您可以实例化内置对象.构造函数 .new 并没有真正记录在案,但是源代码很明显它期望什么参数.

If the code needs a "real* Distro object, for some reason, you can instantiate the built-in one. The constructor .new isn't really documented, but the source code makes it pretty obvious what arguments it expects.

这篇关于更改 $*DISTRO 值以进行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆