Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mocking tools outside/without a package ? #1899

Closed
homer3018 opened this issue Nov 15, 2023 · 1 comment
Closed

Mocking tools outside/without a package ? #1899

homer3018 opened this issue Nov 15, 2023 · 1 comment

Comments

@homer3018
Copy link

Hello,

Within a Shiny app, I'm sourcing a file with all my functions, and I'm using testthat to test these. It's all good until I need to do some mocking and I can't quite figure out how to use the new functions with_mocked_bindingsand local_mocked_bindings.

Assuming I have the following functions :

my_function <- function(){
  0
}

mocked_fn <- function(){
  1
}

I'd expect this test to succeed :

test_that("Mocking a function works",{
  local_mocked_bindings(my_function = mocked_fn)
  
  expect_equal(my_function(), 1)

})

Unfortunately I keep running into Error in 'dev_package()': No packages loaded with pkgload. My sort of suspicion here is that these two functions can only work from within a package environment.

Is my understanding correct ? If so would there be a way of achieving my goal ?

Thanks ahead of time for the support !

@hadley
Copy link
Member

hadley commented Nov 28, 2023

Yeah, those functions only work inside a package sorry. I think the easiest way to solve your problem would be to put your R scripts in R/ directory and your tests in tests/testthat/ directory and add a minimal DESCRIPTION 😄

@hadley hadley closed this as completed Nov 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants