Skip to content

Stubbing out a function called within S3 dispatch #43

Closed

Description

@nbenn

I'm running into issues trying to mock a function with S3 dispatch involved. Consider the following setting:

gen <- function(x) UseMethod("gen")

met <- function(x) foo(x)

foo <- function(x) stop("foo")

.S3method("gen", "cls", met)

x <- structure(123, class = "cls")

gen(x)
#> Error in foo(x) : foo

I would now like to be able to call gen(x) is a test where foo() is stubbed out to avoid throwing the error. I assume this is currently not possible using mockery::stub()?

mockery::stub(gen, "foo", "bar", depth = 1)

gen(x)
#> Error in foo(x) : foo

mockery::stub(gen, "foo", "bar", depth = 2)

gen(x)
#> Error in foo(x) : foo

mockery::stub(gen, "foo", "bar", depth = 3)

gen(x)
#> Error in foo(x) : foo

# etc...

Activity

  1. reopened this on Nov 28, 2020
  2. jimhester commented on Dec 11, 2020

    @jimhester
    Member

    This limitation doesn't really seem to cause many issues in practice, you usually would mock the generic rather than a method.

  3. nbenn commented on Dec 11, 2020

    @nbenn
    Author

    Well ok, for whatever reason I was in the situation that I had written some tests using testthat::with_mock() in a scenario like

    x <- structure(123, class = "cls")
    
    testthat::with_mock(foo = function(x) "yay!", gen(x))
    #> [1] "yay!"

    Then I switched to testthat 3e and found that with the current state of things I was not able to do the above using the advertised alternatives. Fine for me if you say this is not in-scope for 'mockery'. I'll close then.

  4. MichaelChirico commented on Mar 9, 2022

    @MichaelChirico
    Contributor

    I just ran into this myself. It's not clear how to use mockery to replace with_mock here:

    https://github.com/r-lib/lintr/blob/aafba478622c6f43c3fb329c69945c36a4d94b68/tests/testthat/test-ci.R#L47-L49

        with_mock(github_comment = function(x, ...) cat(x, "\n"), .env = asNamespace("lintr"), {
          expect_output(print(l), "*warning:*", fixed = TRUE)
        })
    

    All of the following failed:

    foo <- function(x, ...) cat(x, "\n")
    mockery::stub(lintr:::print.lints, "github_comment", foo) # lintr:::github_comment executes
    mockery::stub(lintr:::print.lints, "github_comment", foo, depth = 2) # error: cannot add bindings to a locked environment
    mockery::stub(print, "github_comment", foo, depth = 2L) # weird error "use of NULL environment is defunct"
    mockery::stub(print, "github_comment", foo) # lintr:::github_comment executes
    
  5. nbenn commented on Mar 11, 2022

    @nbenn
    Author

    @MichaelChirico I started to roll my own, as I was not happy with what {mockery} and {mockr} offered in terms of functionality: {mockthat}. Has worked fine for my own use so far, but I'm sure there are edge-cases, that will break things for my approach too. Happy to take feedback/issues/etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions