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

Vignette on testing challenging functions #1265

Open
maelle opened this issue Dec 3, 2020 · 17 comments
Open

Vignette on testing challenging functions #1265

maelle opened this issue Dec 3, 2020 · 17 comments

Comments

@maelle
Copy link
Contributor

maelle commented Dec 3, 2020

At the moment the only reference to mockery is via the deprecation message of former mocking functions.

Could there be a link to mockery maybe from the README?

@hadley

This comment was marked as outdated.

@maelle

This comment was marked as outdated.

@hadley

This comment was marked as outdated.

@maelle
Copy link
Contributor Author

maelle commented Dec 3, 2020

https://blog.r-hub.io/2019/10/29/mocking/ was relatively popular (well for an R-hub post 🙂 ) so I think a vignette about such topics would be awesome.

@hadley hadley changed the title Add a prominent link to mockery? Vignetting on testing challenging functions Dec 4, 2020
@nbenn

This comment was marked as outdated.

@krlmlr

This comment was marked as outdated.

@maelle
Copy link
Contributor Author

maelle commented Jan 12, 2021

Regarding plots, some guidance on testthat visual snapshots vs vdiffr (or how the two tools complement each other) would be nice.

@hadley hadley changed the title Vignetting on testing challenging functions Vignette on testing challenging functions Sep 13, 2021
@hadley
Copy link
Member

hadley commented Jan 5, 2022

  • Use of expect_snapshot() for length-y text output
  • Plus use of mocking and/or transform to eliminate spurious differences

@hadley
Copy link
Member

hadley commented Sep 15, 2023

I made this list for a recent workshop on testing:

  • Output affected by RNG — withr::local_seed()
  • Output affected by other external state — withr::local_options() / withr::local_envvar()
  • Graphical output — https://vdiffr.r-lib.org/
  • Errors & other user facing text — snapshots
  • HTTP responses — httr2 mocking + httptest2
  • Interactivity — mocking

Are there any other major classes of challenge that you can think of?

@maelle
Copy link
Contributor Author

maelle commented Dec 19, 2023

Regarding interactivity (and other examples like "code that should be run if there is no internet connection"), maybe also mention escape hatches? https://blog.r-hub.io/2023/01/23/code-switch-escape-hatch-test/

@hadley
Copy link
Member

hadley commented Dec 19, 2023

@maelle in that case (especially with local_mocked_bindings()), mocking feels like a clear win.

@maelle
Copy link
Contributor Author

maelle commented Feb 19, 2025

Interactivity — mocking

The mockery package supported returning multiple return values. We encountered this use case in a package under review at rOpenSci, where readline() is mocked.
My best guess is that you'd replace this behavior by using testthat's mocking + a function factory but I'm not sure.

@hadley
Copy link
Member

hadley commented Feb 19, 2025

@maelle testthat could maybe provide a helper for that case:

mock_sequence <- function(values) {
  force(values)
  i <- 1
  function(...) {
    value <- values[[i]]
    i <<- i + 1
    value
  } 
}

local_mocked_bindings(readline = mock_sequence(c("3", "This is a note", "n")))

@maelle
Copy link
Contributor Author

maelle commented Feb 20, 2025

Yes that would be helpful 😸 Happy to make a PR if relevant.
Yesterday I tried looking for use cases of mockery's "multiple return values" but it's not easy to search for 😅

@hadley
Copy link
Member

hadley commented Feb 20, 2025

A PR would be great. I think the main challenge is what to call it. Maybe it's reasonable to have a new series of functions that start with mock?

Probably also need to error if i > length(values).

@maelle
Copy link
Contributor Author

maelle commented Feb 21, 2025

mock_output_sequence()?

It reminds me of an use case in HTTP testing, where one mocks an API returning a 503 then a 200 status code for instance. It works with vcr or webmockr but it doesn't really have a name there.

I'll make a PR soon-ish.

@maelle
Copy link
Contributor Author

maelle commented Feb 21, 2025

#2061

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

No branches or pull requests

4 participants