pretend_now_is: Pretend now is some other time.

Description Usage Arguments Details Value Examples

View source: R/pretend_now_is.R

Description

This method stubs the built-in Sys.time, Sys.Date, and date methods with an overwritten value for use in unit tests.

Usage

1

Arguments

time

Date or POSIXct or character. For example, Sys.time() + as.difftime(1, units = "days") or "1 day from now".

expr

expression. The expression to evaluate while pretending the current time is time.

Details

Either a Date or POSIXct object can be provided, or an English string in the form "<number> <unit>s <from now/ago>" such as:

These are translated into Sys.time() + as.difftime(number, units = unit) (or - in the case of "ago").

Value

the value of expr.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
pretend_now_is(Sys.time() + as.difftime(1, units = "days"), {
 cat("It's UNIX time ", Sys.time(), ", tomorrow!\n", sep = "")
})

now <- Sys.time()
pretend_now_is("10 minutes from now", {
  stopifnot(all.equal(
   as.integer(as.difftime(Sys.time() - now, units = "minutes")),
   10))
})

robertzk/testthatsomemore documentation built on Aug. 15, 2017, 7:45 a.m.