Skip to content

Commit ed78cae

Browse files
hadleymaellemcol
authored
Incorporate doc feedback from @maelle (#2231)
--------- Co-authored-by: Maëlle Salmon <[email protected]> Co-authored-by: Marco Colombo <[email protected]>
1 parent a5a6df2 commit ed78cae

File tree

7 files changed

+72
-6
lines changed

7 files changed

+72
-6
lines changed

R/mock-oo.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
#' Mock S3 and S4 methods
22
#'
3+
#' @description
34
#' These functions allow you to temporarily override S3 and S4 methods that
45
#' already exist. It works by using [registerS3method()]/[setMethod()] to
56
#' temporarily replace the original definition.
67
#'
8+
#' Learn more about mocking in `vignette("mocking")`.
9+
#'
710
#' @param generic A string giving the name of the generic.
811
#' @param signature A character vector giving the signature of the method.
912
#' @param definition A function providing the method definition.
@@ -67,12 +70,15 @@ local_mocked_s4_method <- function(
6770

6871
#' Mock an R6 class
6972
#'
73+
#' @description
7074
#' This function allows you to temporarily override an R6 class definition.
7175
#' It works by creating a subclass then using [local_mocked_bindings()] to
7276
#' temporarily replace the original definition. This means that it will not
7377
#' affect subclasses of the original class; please file an issue if you need
7478
#' this.
7579
#'
80+
#' Learn more about mocking in `vignette("mocking")`.
81+
#'
7682
#' @export
7783
#' @param class An R6 class definition.
7884
#' @param public,private A named list of public and private methods/data.

R/mock2.R

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
#' state (i.e. reading a value from a file or a website, or pretending a package
88
#' is or isn't installed).
99
#'
10-
#' These functions represent a second attempt at bringing mocking to testthat,
11-
#' incorporating what we've learned from the mockr, mockery, and mockthat
12-
#' packages.
10+
#' Learn more in `vignette("mocking")`.
1311
#'
1412
#' # Use
1513
#'

man/local_mocked_bindings.Rd

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/local_mocked_r6_class.Rd

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/local_mocked_s3_method.Rd

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vignettes/challenging-tests.Rmd

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,25 @@ test_that("user must respond y or n", {
134134
})
135135
```
136136

137+
If you don't care about reproducing the output of `continue()` and just want to recreate its return value, you can use `mock_output_sequence()`. This creates a function that returns the input supplied to `mock_output_sequence()` in sequence: the first input at the first call, the second input at the second call, etc. The following code shows how it works and how you might use it to test `readline()`:
138+
139+
```{r}
140+
f <- mock_output_sequence(1, 12, 123)
141+
f()
142+
f()
143+
f()
144+
```
145+
146+
And
147+
148+
```{r}
149+
test_that("user must respond y or n", {
150+
local_mocked_bindings(readline = mock_output_sequence("x", "y"))
151+
expect_true(continue("This is dangerous"))
152+
})
153+
```
154+
155+
137156
If you were testing the behavior of some function that uses `continue()`, you might want to mock `continue()` instead of `readline()`. For example, the function below requires user confirmation before overwriting an existing file. In order to focus our tests on the behavior of just this function, we mock `continue()` to return either `TRUE` or `FALSE` without any user messaging.
138157

139158
```{r}

vignettes/custom-expectation.Rmd

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,47 @@ This vignette shows you how to write your own expectations. Custom expectations
2121

2222
In this vignette, you'll learn about the three-part structure of expectations, how to test your custom expectations, see a few examples, and, if you're writing a lot of expectations, learn how to reduce repeated code.
2323

24+
## Do you need it?
25+
26+
But before you read the rest of the vignette and dive into the full details of creating a 100% correct expectation, consider if you can get away with a simpler wrapper. If you're just customising an existing expectation by changing some defaults, you're fine:
27+
28+
```{r}
29+
expect_df <- function(tbl) {
30+
expect_s3_class(tbl, "data.frame")
31+
}
32+
```
33+
34+
If you're combining multiple expectations, you can introduce a subtle problem. For example, take this expectation from tidytext:
35+
36+
```{r}
37+
# from tidytext
38+
expect_nrow <- function(tbl, n) {
39+
expect_s3_class(tbl, "data.frame")
40+
expect_equal(nrow(tbl), n)
41+
}
42+
```
43+
44+
If we use it in a test you can see there's an issue:
45+
46+
```{r}
47+
#| error: true
48+
test_that("success", {
49+
expect_nrow(mtcars, 32)
50+
})
51+
test_that("failure 1", {
52+
expect_nrow(mtcars, 30)
53+
})
54+
test_that("failure 2", {
55+
expect_nrow(matrix(1:5), 2)
56+
})
57+
```
58+
59+
Each of these tests contain a single expectation, but they report a total of two successes and failures. It would be confusing if testthat didn't report these numbers correctly. But as a helper in your package, it's probably not a big deal.
60+
61+
You might also notice that these failures generate a backtrace whereas built-in expectations don't. Again, it's not a big deal because the backtrace is correct, it's just not needed.
62+
63+
These are both minor issues, so if they don't bother you, you can save yourself some pain by not reading this vignette 😀.
64+
2465
## Expectation basics
2566

2667
An expectation has three main parts, as illustrated by `expect_length()`:

0 commit comments

Comments
 (0)