Skip to content

Commit 08505d5

Browse files
committed
Incorporate doc feedback from @maelle
1 parent 387580b commit 08505d5

File tree

9 files changed

+62
-8
lines changed

9 files changed

+62
-8
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,4 @@ Config/testthat/parallel: true
5757
Config/testthat/start-first: watcher, parallel*
5858
Encoding: UTF-8
5959
Roxygen: list(markdown = TRUE, r6 = FALSE)
60-
RoxygenNote: 7.3.2
60+
RoxygenNote: 7.3.3

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/expect_vector.Rd

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

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 each time it's called returns the next input supplied to `mock_output_sequence()`. 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: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,35 @@ 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 fully 100% correct expectation, consider if you can get away with a simpler wrapper. For example, take this expectation from tidytext:
27+
28+
```{r}
29+
# from tidytext
30+
expect_nrow <- function(tbl, n) {
31+
expect_s3_class(tbl, "data.frame")
32+
expect_equal(nrow(tbl), n)
33+
}
34+
```
35+
36+
If we use it in a test you can see there's a subtle problem:
37+
38+
```{r}
39+
#| error: true
40+
test_that("success", {
41+
expect_nrow(mtcars, 32)
42+
})
43+
test_that("failure 1", {
44+
expect_nrow(mtcars, 30)
45+
})
46+
test_that("failure 2", {
47+
expect_nrow(matrix(1:5), 2)
48+
})
49+
```
50+
51+
Each of these tests contain a single expectation, but they report a total of 2 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 and you save yourself some pain by not reading this vignette 😀. You might also notice that these failures generate a backtrace whereas most failures don't. Again this is not a big deal, and the backtrace is correct, it's just not needed.
52+
2453
## Expectation basics
2554

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

0 commit comments

Comments
 (0)