Skip to content

Instantly share code, notes, and snippets.

@sunaot
Created March 29, 2013 13:47
Show Gist options
  • Save sunaot/5270966 to your computer and use it in GitHub Desktop.
Save sunaot/5270966 to your computer and use it in GitHub Desktop.

setup を DRY にしたい

# こんな支援モジュールを書く
use strict;
use warnings;

{
    package Foo::Bar;
    sub hello { undef; }
}

{
    package StubbedTest;
    use parent qw(Exporter);
    our @EXPORT = qw(stubbed_test);
    
    use Test::More;
    use Test::Mock::Guard qw(mock_guard);
    
    sub stubbed_test {
        my ($name, $subtest) = @_;
        my $stub = mock_guard('Foo::Bar', { hello => sub { 'hello'; } });
        subtest $name, $subtest;
    }
}

1;
# テストするほうはこうして使う
use Test::More;
use lib 't/lib';
use StubbedTest;

stubbed_test 'stubbed test' => sub {
    is Foo::Bar::hello(), 'hello';
};

done_testing;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment