79597808

Date: 2025-04-29 07:28:00
Score: 2
Natty:
Report link

This seems to work fine within a test_that expression. Can this cause any problems?


random_function <- function(in_dir) {
  return(file.access(in_dir, 0))
}
  

testthat::test_that("Base function is mocked correctly", {
  
  testthat::expect_true(random_function("random/directory/that/does/not/exist") == -1)
  
  testthat::local_mocked_bindings(
    file.access = function(in_dir, mode) {
      return(0)
    }, .package = "base")
  
  testthat::expect_true(random_function("random/directory/that/does/not/exist") == 0)
  
})


testthat::test_that("Base function is not mocked anymore", {
  
  testthat::expect_true(random_function("random/directory/that/does/not/exist") == -1)

})


Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Ends in question mark (2):
  • Low reputation (1):
Posted by: Luxington