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

stub depth > 1 not working for R6 classes #62

Closed
adamborondy opened this issue Aug 23, 2022 · 2 comments
Closed

stub depth > 1 not working for R6 classes #62

adamborondy opened this issue Aug 23, 2022 · 2 comments

Comments

@adamborondy
Copy link

adamborondy commented Aug 23, 2022

I am trying to use mockery::stub() in combination with an R6 class, which works out fine for functions at a depth of 1. Here is a simplified R6 example:

library(mockery)

stubTest <- R6::R6Class(classname = "stubTest",
  public =
    list(
      sum = 0,
      add_x = function() {
        self$sum <- self$sum + self$add_y()
        invisible(self)
      },
      add_y = function() {
        self$add_z()
      },
      add_z = function() {
        5
      }
    )
)

I can successfully stub self$add_y like this:

test_that('Stubbing test depth 1', {
  st <- stubTest$new()
  stub(st$add_x, 'self$add_y', 10, depth = 1)
  st$add_x()
  expect_equal(st$sum, 10)
})

However I cannot do the same for self$add_z at a depth of 2. This results in an actual value of 5 as expected without stubbing:

test_that('Stubbing test depth 2', {
  st <- stubTest$new()
  stub(st$add_x, 'self$add_z', 10, depth = 2)
  st$add_x()
  expect_equal(st$sum, 10)
})

I'm not sure if this feature is applicable to R6 classes in this way, so I appreciate any feedback!

@hadley
Copy link
Member

hadley commented Oct 31, 2023

mockery is no longer under active development as we now recommend using testthat::local_mocked_bindings() instead. That doesn't yet support mocking R6 methods, but we could certainly consider it if it's a popular need.

@hadley hadley closed this as completed Oct 31, 2023
@hadley
Copy link
Member

hadley commented Oct 31, 2023

I've filed an issue to look into this for testthat: r-lib/testthat#1892

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