webmockr icon indicating copy to clipboard operation
webmockr copied to clipboard

Make it easy to get count of calls matching a given stub

Open sckott opened this issue 1 year ago • 0 comments

seems quite easy with python's pook (mock.calls), see first example block at https://github.com/h2non/pook?tab=readme-ov-file#examples

in this package, perhaps something like

# use a function so user doesn't have to figure out the 
# incantation for the R6 object
call_count <- function(stub) {
    stub$counter$count()
}

# possibly use the last stub used by default? but would have to show
# the stub then (`last_stub_used` doesn't exist yet)
call_count <- function(stub = last_stub_used()) {
    stub$counter$count()
}

# optionally just give back counts for all stubs if `stub` is `NULL`?
# this uses cli so would have to add that dep, but it doesn't have deps itself
call_count <- function(stub = NULL) {
  reg <- stub_registry()
  stubs <- reg$request_stubs
  cli_alert_info(style_underline(style_bold("count --- stub")))
  for (stub in stubs) {
    cat(
      symbol$info, " ",
      style_bold(stub$counter$count()), " --- ",
      stub$to_s(), "\n",
      sep = ""
    )
  }
}

sckott avatar Oct 08 '24 16:10 sckott