.\" Automatically generated by Pod::Man 4.11 (Pod::Simple 3.35) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Test::MockDateTime 3pm" .TH Test::MockDateTime 3pm "2019-10-20" "perl v5.30.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Test::MockDateTime \- mock DateTime\->now calls during tests .SH "VERSION" .IX Header "VERSION" version 0.02 .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 3 \& use Test::More; \& use Test::MockDateTime; \& use DateTime; \& \& on \*(Aq2013\-01\-02 03:04:05\*(Aq => sub { \& # inside this block all calls to DateTime::now \& # will report a mocked date. \& \& my $now = DateTime\->now; \& is $now\->ymd, \*(Aq2013\-01\-02\*(Aq, \*(Aqoccured now\*(Aq; \& }; \& \& done_testing; .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" Getting the current time sometimes is not very helpful for testing scenarios. Instead, if you could obtain a known value during the runtime of a testcase will make your results predictable. .PP Why another Date Mocker? I wanted something simple with a very concise usage pattern and a mocked date should only exist and stay constant inside a scope. After leaving the scope the current time should be back. This lead to this tiny module. .PP This simple module allows faking a given date and time for the runtime of a subsequent code block. By default the \f(CW\*(C`on\*(C'\fR keyword is exported into the namespace of the test file. The date to get mocked must be in a format that is recognized by DateTime::Format::DateParse. .PP .Vb 1 \& on \*(Aq2013\-01\-02 03:04:05\*(Aq, sub { ... }; .Ve .PP is basically the same as .PP .Vb 4 \& { \& my $now = DateTime::Format::DateParse\->parse_datetime( \& \*(Aq2013\-01\-02 03:04:05\*(Aq \& ); \& \& local *DateTime::now = sub { $now\->clone }; \& \& ... everything from code block above \& } .Ve .PP A drawback when relying on this module is that you must know that the module you are testing uses \f(CW\*(C`DateTime\->now\*(C'\fR to obtain the current time. .SH "FUNCTIONS" .IX Header "FUNCTIONS" .ie n .SS "on $date_and_time, \e&code" .el .SS "on \f(CW$date_and_time\fP, \e&code" .IX Subsection "on $date_and_time, &code" mocks date and time and then executes code .SH "CAVEATS" .IX Header "CAVEATS" This module only mocks calls to \f(CW\*(C`DateTime\->now\*(C'\fR. All other ways to obtain a current time are not touched. .SH "SEE ALSO" .IX Header "SEE ALSO" There are some alternatives. Depending on the environment you might consider using one of them instead. .IP "Test::MockTime" 4 .IX Item "Test::MockTime" Very universal, overwrites several subs at compile time and allows to set a fixed or ticking time at any place in your code. .IP "Time::Mock" 4 .IX Item "Time::Mock" Also allows to set a time at various places inside your code. .IP "Test::MockTime::DateCalc" 4 .IX Item "Test::MockTime::DateCalc" Mocks several Date::Calc functions. .IP "Time::Fake" 4 .IX Item "Time::Fake" Also overwrites several subs at compile time. .SH "AUTHOR" .IX Header "AUTHOR" Wolfgang Kinkeldei, .SH "LICENSE" .IX Header "LICENSE" This library is free software. You can redistribute it and/or modify it under the same terms as Perl itself.