如何使这些测试更加 DRY?

发布于 2024-11-23 20:18:06 字数 1176 浏览 1 评论 0原文

我目前在几个测试文件的开头有以下内容,但它非常不干燥。但我不太确定将其移动到自己的文件中的最佳方法是什么。有什么建议吗?

#!/usr/bin/perl
use strict;
use warnings;
BEGIN {
    use Test::More;
    use namespace::clean qw( pass );
}
use FindBin;
use Cwd qw( realpath );
use Dancer qw( :syntax );
use Test::WWW::Mechanize::PSGI;
set apphandler => 'PSGI';

my $appdir = realpath( "$FindBin::Bin/.." );
my $t = Test::WWW::Mechanize::PSGI->new(
    app => sub {
        my $env = shift;
        setting(
            appname => 'MyApp',
            appdir => $appdir,
        );
        load_app 'MyApp';
        config->{environment} = 'test';
        Dancer::Config->load;
        my $request = Dancer::Request->new( env => $env );
        Dancer->dance( $request );
    }
);
$t->agent('test');

$t->get_ok('/login') or diag $t->content;

$t->submit_form_ok({
    form_name =>'loginform',
    fields    => {
        username => 'myuser',
        password => 'foo',
    },
}, 'login ok' );

### END BOILERPLATE ###

更新

不幸的是,我将其移至库中的部分问题是,一旦完成,代码就会停止工作。我尝试将其封装到子例程中并返回 $t 但这似乎不起作用。我试图弄清楚什么到底需要进入图书馆以及什么到底需要进入测试。

I currently have the following at the beginning of several test files, but it's very not DRY. But I'm not really sure what the best way to move this into its own file is. Any suggestions?

#!/usr/bin/perl
use strict;
use warnings;
BEGIN {
    use Test::More;
    use namespace::clean qw( pass );
}
use FindBin;
use Cwd qw( realpath );
use Dancer qw( :syntax );
use Test::WWW::Mechanize::PSGI;
set apphandler => 'PSGI';

my $appdir = realpath( "$FindBin::Bin/.." );
my $t = Test::WWW::Mechanize::PSGI->new(
    app => sub {
        my $env = shift;
        setting(
            appname => 'MyApp',
            appdir => $appdir,
        );
        load_app 'MyApp';
        config->{environment} = 'test';
        Dancer::Config->load;
        my $request = Dancer::Request->new( env => $env );
        Dancer->dance( $request );
    }
);
$t->agent('test');

$t->get_ok('/login') or diag $t->content;

$t->submit_form_ok({
    form_name =>'loginform',
    fields    => {
        username => 'myuser',
        password => 'foo',
    },
}, 'login ok' );

### END BOILERPLATE ###

update

unfortunately part of my problem with moving this off into a library is that as soon as I've done that the code stops working. I tried encapsulating it into a subroutine and returning $t but that doesn't appear to work. I'm trying to figure out what exactly needs to go into the library and what exactly needs to go into the test.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

弥枳 2024-11-30 20:18:06

使其成为一个模块(例如 t::MyApp),将 my $t 更改为 our $t,并让模块导出 $t。 (您还可以编写自定义 import 方法来在测试脚本中打开严格和警告。)

Make it a module (say t::MyApp), change my $t to our $t, and have the module export $t. (You could also write a custom import method to turn on strict & warnings in your test script.)

慢慢从新开始 2024-11-30 20:18:06

您可以创建一个包含这些行的 .pm 模块,并使用一些面向对象的代码从样板代码中获取 $t 和其他信息,然后 在您的测试中使用它。

You could create a .pm module that includes these lines, with some object-oriented code to obtain the $t and other information from the boilerplate code, and then use it from your tests.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文