Hi, nice to meet you all!
I am trying to use with_mock right now and this is what happens. (my environment is described at the end of this post)
(1) with_mock(testthat::auto_test=function(...)2, base::cat=function(...)3, {print(auto_test);print(cat);T})
As far as I understand, this line should print two mocked functions. Instead, it produces the original testthat::auto_test
with_mock(
testthat::auto_test=function(...)2,base::cat=function(...)3, {print(auto_test);print(cat);T})
function (code_path, test_path, reporter = "summary", env = test_env())
{
reporter <- find_reporter(reporter)
[many lines removed]
}
function(...)3
[1] TRUE
(2) with_mock(plyr::alply=function(...)2, base::cat=function(...)3, {print(alply);print(cat);T})
Without plyr loaded this line produces an error message: "Error in print(alply) : object 'alply' not found"
If I load plyr, I get its original alply body instead of my mock.
(3) I've tried to understand if I'm using your code in the right way. The only thing that I saw and that surprised was the result of calling asNamespace('plyr') when plyr is not yet loaded:
asNamespace('plyr')
(4) I have check also that after loading this particular package asNamespace seems to be returning the correct environment:
> library(plyr)
> identical(asNamespace('plyr'), environment(alply))
[1] TRUE
When I try to overwrite anything in this environment, R doesn't allow me to, unless I call unlockBinding.
> x=environment(alply)
> y=asNamespace('plyr')
> y$alply<-1
Error in y$alply <- 1 : cannot change value of locked binding for 'alply'
> unlockBinding('alply',y)
> y$alply<-1
> x$alply
[1] 1
As you can see, both x and y refer to the same object alply.
But when I try to read its value from the global env, I get the original value:
> alply
function (.data, .margins, .fun = NULL, ..., .expand = TRUE,
[many lines removed]
<environment: namespace:plyr>
(5) Is there something I should do to be able to mock functions in the plyr package?
(6) environment
R version 3.1.1 (2014-07-10)
Linux ubuntu 3.13.0-35-generic #62-Ubuntu SMP Fri Aug 15 01:58:42 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
testthat$ git pull
Already up-to-date.
testthat$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
testthat$ cd .. && R CMD INSTALL testthat
I'm loading testhat with this line:
library(testthat, lib.loc='~/R/x86_64-pc-linux-gnu-library/3.1/')