I am unit testing a component that requires user input. How do I tell Test::More
to use some input that I predefined so that I don't need to enter it manually?
我正在测试需要用户输入的组件。如何告诉Test :: More使用我预定义的一些输入,以便我不需要手动输入?
This is what I have now:
这就是我现在拥有的:
use strict;
use warnings;
use Test::More;
use TestClass;
*STDIN = "1\n";
foreach my $file (@files)
{
#this constructor asks for user input if it cannot find the file (1 is ignore);
my $test = TestClass->new( file=> @files );
isa_ok( $test, 'TestClass');
}
done_testing;
This code does press enter but the function is retrieving 0 not 1;
此代码确实按Enter键,但函数检索0而不是1;
2 个解决方案
#1
7
The following minimal script seems to work:
以下最小脚本似乎有效:
#!/usr/bin/perl
package TestClass;
use strict;
use warnings;
sub new {
my $class = shift;
return unless <STDIN> eq "1\n";
bless {} => $class;
}
package main;
use strict;
use warnings;
use Test::More tests => 1;
{
open my $stdin, '<', \ "1\n"
or die "Cannot open STDIN to read from string: $!";
local *STDIN = $stdin;
my $test = TestClass->new;
isa_ok( $test, 'TestClass');
}
Output:
输出:
C:\Temp> t 1..1 ok 1 - The object isa TestClass
#2
16
If the program reads from STDIN
, then just set STDIN
to be the open filehandle you want it to be:
如果程序从STDIN读取,那么只需将STDIN设置为您希望它的打开文件句柄:
#!perl
use strict;
use warnings;
use Test::More;
*STDIN = *DATA;
my @a = <STDIN>;
is_deeply \@a, ["foo\n", "bar\n", "baz\n"], "can read from the DATA section";
my $fakefile = "1\n2\n3\n";
open my $fh, "<", \$fakefile
or die "could not open fake file: $!";
*STDIN = $fh;
my @b = <STDIN>;
is_deeply \@b, ["1\n", "2\n", "3\n"], "can read from a fake file";
done_testing;
__DATA__;
foo
bar
baz
You may want to read more about typeglobs in perldoc perldata
and more about turning strings into fake files in the documentation for open
(look for "Since v5.8.0, perl has built using PerlIO by default.") in perldoc perlfunc
.
您可能想要阅读有关perldoc perldata中的typeglobs的更多信息,以及更多关于在开放文档中将字符串转换为伪文件的更多信息(查看“自v5.8.0以来,Perl默认使用PerlIO构建。”)perldoc perlfunc。
#1
7
The following minimal script seems to work:
以下最小脚本似乎有效:
#!/usr/bin/perl
package TestClass;
use strict;
use warnings;
sub new {
my $class = shift;
return unless <STDIN> eq "1\n";
bless {} => $class;
}
package main;
use strict;
use warnings;
use Test::More tests => 1;
{
open my $stdin, '<', \ "1\n"
or die "Cannot open STDIN to read from string: $!";
local *STDIN = $stdin;
my $test = TestClass->new;
isa_ok( $test, 'TestClass');
}
Output:
输出:
C:\Temp> t 1..1 ok 1 - The object isa TestClass
#2
16
If the program reads from STDIN
, then just set STDIN
to be the open filehandle you want it to be:
如果程序从STDIN读取,那么只需将STDIN设置为您希望它的打开文件句柄:
#!perl
use strict;
use warnings;
use Test::More;
*STDIN = *DATA;
my @a = <STDIN>;
is_deeply \@a, ["foo\n", "bar\n", "baz\n"], "can read from the DATA section";
my $fakefile = "1\n2\n3\n";
open my $fh, "<", \$fakefile
or die "could not open fake file: $!";
*STDIN = $fh;
my @b = <STDIN>;
is_deeply \@b, ["1\n", "2\n", "3\n"], "can read from a fake file";
done_testing;
__DATA__;
foo
bar
baz
You may want to read more about typeglobs in perldoc perldata
and more about turning strings into fake files in the documentation for open
(look for "Since v5.8.0, perl has built using PerlIO by default.") in perldoc perlfunc
.
您可能想要阅读有关perldoc perldata中的typeglobs的更多信息,以及更多关于在开放文档中将字符串转换为伪文件的更多信息(查看“自v5.8.0以来,Perl默认使用PerlIO构建。”)perldoc perlfunc。