简体   繁体   English

如何在Perl测试中模拟%ENV?

[英]How can I mock %ENV in Perl tests?

I'm trying to retrofit some tests using Test::More to legacy code and I've bumped into a bit of a snag. 我正在尝试使用Test::More对遗留代码进行一些测试改造,但遇到了一些麻烦。 I don't seem to be able to set %ENV in the test module. 我似乎无法在测试模块中设置%ENV The called function definitely uses this variable so %ENV doesn't seem to be carried across to the test object. 被调用函数肯定使用了此变量,因此%ENV似乎不会传递到测试对象。

#!/usr/bin/perl

use strict; use warnings;
use Test::More qw(no_plan);

BEGIN {
    $ENV{HTTP_WWW_AUTHENTICATE} = 
        'WWW-Authenticate: MyType realm="MyRealm",userid="123",password="abc"';
    use_ok('Util');
}

$ENV{HTTP_WWW_AUTHENTICATE} = 
    'WWW-Authenticate: MyType realm="MyRealm",userid="123",password="abc"';

printf qq{get_authentication_info = "%s"\n}, get_authentication_info();
ok(get_authentication_info(), 'Get authentication info');

I keep getting... 我不断得到...

perl t\Util.t
ok 1 - use Util;
Use of uninitialized value in concatenation (.) or string at t\Util.t line 14.
get_authentication_info = ""

As with all things Perl, I'm pretty sure that some one has done this before. 与Perl一样,我很确定以前有人做过。

UPDATE: Thanks to all for your help 更新:谢谢大家的帮助

The problem was between the keyboard & chair ... My test data was just plain wrong It needed to be 问题出在键盘和椅子之间……我的测试数据完全错误。

$ENV{HTTP_WWW_AUTHENTICATE} = 
    'MyType realm="MyRealm",userid="123",password="abc"';

As Sinan said, the $ENV{...} lines are commented out, so it can't work. 正如Sinan所说, $ENV{...}行已被注释掉,因此它不起作用。 But if you want really testable code, I'd suggest to make the get_authentication_info function take a hash as an argument. 但是,如果您想要真正可测试的代码,我建议使get_authentication_info函数将哈希作为参数。 That way you can test it without setting the global variable, and in the real code you can pass the real enviromnent hash. 这样,您可以在不设置全局变量的情况下对其进行测试,并且可以在真实代码中传递真实的环境哈希值。 Global state will always become a problem eventually. 最终,全球状态将始终成为一个问题。

Why are the lines setting $ENV{HTTP_WWW_AUTHENTICATE} commented out? 为什么设置$ENV{HTTP_WWW_AUTHENTICATE}注释掉?

Also, what are the specs for get_authentication_info() ? 另外, get_authentication_info()的规范是什么?

Agreed with Lukáš -- get your global environment (and perform validity checks etc) all in one place, such as in its own method, and then pass those values to all other methods that need it. 与Lukáš达成协议-将您的全局环境(并执行有效性检查等)全部集中在一个地方,例如使用自己的方法,然后将这些值传递给需要它的所有其他方法。 That way in your unit tests you can just drop in a replacement method that determines the environment and config variables in a different way (such as from a file, or directly set at the top of your test script). 这样,在单元测试中,您只需使用一种替代方法即可,该方法可以以不同的方式(例如,从文件或直接在测试脚本的顶部设置)来确定环境和配置变量。

What does 是什么

get_authentication_info()

return? 返回?

My guess is nothing. 我的猜测没什么。

If this is always true, then line 14 will always return the "Use of uninitialized value..." warning. 如果始终都是这样,则第14行将始终返回“使用未初始化的值...”警告。

If you expect a value, you need to investigate why get_authentication_info() is failing? 如果期望一个值,则需要调查为什么get_authentication_info()失败?

Try setting the env variable before BEGIN. 尝试在开始之前设置env变量。

If not try this: 如果没有尝试,请执行以下操作:

  1. First, go to a command prompt and set the env var there. 首先,转到命令提示符并在此处设置env var。 Then run your script. 然后运行您的脚本。 If the tests pass. 如果测试通过。 Then as you predicted, the problem is with setting the env var. 然后,正如您所预料的那样,问题出在设置环境变量。

  2. If the tests fail, then the problem lies some where else (probably in get_authentication_info ). 如果测试失败,则问题出在其他地方(可能在get_authentication_info )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM