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

test: use function factory instead of {mockery} #207

Merged
merged 3 commits into from
Feb 25, 2025
Merged

Conversation

maelle
Copy link
Member

@maelle maelle commented Feb 19, 2025

Reference: https://adv-r.hadley.nz/function-factories.html#factory-fundamentals

mock_factory <- function(...) {
  i <- 0
  
  function(...) {
    things <- c("3", "This is a note", "n")
    i <<- i + 1
    things[i]
  }
}

new_mock <- mock_factory()
new_mock()
#> [1] "3"
new_mock()
#> [1] "This is a note"
new_mock()
#> [1] "n"
new_mock()
#> [1] NA

Created on 2025-02-19 with reprex v2.1.1

@maelle
Copy link
Member Author

maelle commented Feb 19, 2025

I must say I still have trouble wrapping my head around function factories 🤯

@maelle maelle mentioned this pull request Feb 19, 2025
29 tasks
Copy link

codecov bot commented Feb 19, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

@@ -1,9 +1,19 @@
test_that("user_categorisation works with valid input", {
# create a mock object that returns user inputs in sequence
mock_readline <- mockery::mock("3", "This is a note", "n")
mock_factory <- function(...) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the ellipsis is actually probably not needed here 🤔

@maelle
Copy link
Member Author

maelle commented Feb 20, 2025

@RayStick see also r-lib/testthat#1265 (comment) -- maybe you could define such a function in a helper file (tests/testthat/helper-mock.R), use it in your package, and if testthat ends up providing its own version of the function, you could replace your homemade function with that?

This way you could already drop mockery.

@RayStick
Copy link
Contributor

RayStick commented Feb 21, 2025

@maelle thanks for your work on this! I am struggling to get my head around it at the moment

@RayStick
Copy link
Contributor

Ah - think I got it! Working on it this morning

@maelle
Copy link
Member Author

maelle commented Feb 25, 2025

The PR to testthat is making progress, so hopefully in a little while you can use that function (and have mapmetadata depend on the dev version of testthat) r-lib/testthat#2061

@RayStick
Copy link
Contributor

@maelle see my commit here: af5cd48
Shall we merge this in the time being, or best to wait? (if happy to merge, please remove draft PR status)
devtools::check() all pass :)

@maelle maelle marked this pull request as ready for review February 25, 2025 08:45
@maelle
Copy link
Member Author

maelle commented Feb 25, 2025

It's really nice especially with the explaining comments.

Copy link
Contributor

@RayStick RayStick left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @maelle

@RayStick RayStick merged commit 1cdd58d into ropensci:main Feb 25, 2025
9 checks passed
@RayStick
Copy link
Contributor

@all-contributors add @maelle for code

Copy link
Contributor

@RayStick

I've put up a pull request to add @maelle! 🎉

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

Successfully merging this pull request may close these issues.

2 participants