I've been trying to set up a mocking framework to use in testing, in conjunction with testthat. I come from a java background. What I've settled on is calling methods(class=klass), adding a "mock" to the name and assigning a function that returns null to the new name in the base environment. I've needed to assign it to the base environment because the package environments are locked, and when code is executed in the package environment, it doesn't have access to any new environment I give it. Unfortunately, this litters the base environment with meaningless names that I can't then delete. Is this the best approach? Does anybody else use mocking code in their testing? Thank you, Dan PS Some basic code: #' @export mock <- function(klass, ...) { new_methods <- list(...); lapply(methods(class=klass), function(method_name) { if (method_name %in% names(new_methods)) { new_method <- new_methods[[method_name]]; } else { new_method <- function() NULL; } formals(new_method) <- formals(method_name); assign(mocked_name(method_name), new_method, baseenv()); }); } #' @export mocked_object <- function(klass) { x <- list(); class(x) <- mocked_name(klass); return(x); } #' @export clean_base <- function(klass) { lapply(methods(class=mocked_name(klass)), function(name) { assign(name, NULL, envir=baseenv()) }); } mocked_name <- function(name) {paste0(name, "mock")}; [[alternative HTML version deleted]]