mardi 13 mars 2018

How to testing an R function which uses Sys.time()?

I have the following R function within a software package, which outputs Sys.time() to the output for users to know how the algorithm is proceeding:

func = function(num1, num2){
    result = num1 + num2
    return(paste0(' ', as.character(Sys.time()), ':  with result: ', result))
}

An example of using this function is as follows:

> func(2, 2)
[1] " 2018-03-11 07:24:05:  with result: 4"
> 

I need to test this function. Normally, I would use the testthat package:

https://cran.r-project.org/web/packages/testthat/index.html

Question: How does one set the Sys.time() in order to test this function? Are there other methods available to test this?

If there was no Sys.time(), this process would be simple:

library(testthat)
expect_equal(func(2, 2), 4)
expect_equal(func(2, -2), 0)
expect_error(func('a', 'b'))

Aucun commentaire:

Enregistrer un commentaire