Skip to content

Stubbing out a function called within S3 dispatch #43

Closed
@nbenn

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...

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