But I hit a problem when trying to write a unit test for something(). It works with a global function as the callback (such as mean in the below code), but not for a locally defined function (thingy in the below code):
thingy=function(x){ mean(x) }The problem appears to be that functions in a test file are all loaded in a special environment. And, when I thought about it, that makes sense, unit test functions are supposed to be small and independent, and I don't want to be defining a function at the same level as the unit tests.
test.something=function(){
x=1:10
something(mean,x)
something("mean",x)
something( list("mean",x) )
something(thingy,x)
something("thingy",x) #Fails
something( list("thingy",x) ) #Fails
}
I tried a few things, but here is the one that worked:
test.something=function(){So, we explicitly put our thingy() in the global environment, where everyone can find it and make use of it. And we define it inside the test function, no earlier. Removing it at the end is optional, but Murphy's Law states that if we don't then it will end up clashing with something. Late on a Friday afternoon, in six months time, when we've forgotten about this code.
assign("thingy",function(x){ mean(x) }, envir=globalenv())
x=1:10
something(mean,x)
something("mean",x)
something( list("mean",x) )
something(thingy,x)
something("thingy",x)
something( list("thingy",x) )
remove("thingy", envir=globalenv())
}