Skip to content

Commit ec6be19

Browse files
committed
Comment about fail
1 parent 315be42 commit ec6be19

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

vignettes/custom-expectation.Rmd

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ By convention, the first argument to every `expect_` function is called `object`
4949

5050
### Verify the expectation
5151

52-
Now we can check if our expectation is met and call `fail()` if not. The most challenging job here is typically generating the error message because you want it to be as self-contained as possible. This means it should typically give both the expected and actual value, along with the name of the object passed to the expectation. testthat expectations use `sprintf()`, but if you're familiar with {glue}, you might want to use that instead.
52+
Now we can check if our expectation is met and return `fail()` if not. The most challenging job here is typically generating the error message because you want it to be as self-contained as possible. This means it should typically give both the expected and actual value, along with the name of the object passed to the expectation. testthat expectations use `sprintf()`, but if you're familiar with {glue}, you might want to use that instead.
5353

5454
More complicated expectations will have more `if` statements. For example, we might want to make our `expect_length()` function include an assertion that `object` is a vector:
5555

@@ -72,6 +72,8 @@ expect_vector_length <- function(object, n) {
7272
}
7373
```
7474

75+
Note that it's really important to `return(fail())` here. You wont see the problem when interactively testing your function because when run outside of `test_that()`, `fail()` throws an error, causing the function to terminate early. When running inside of `test_that()` however, `fail()` does not stop execution because we want to collect all failures in a given test.
76+
7577
### Pass the test
7678

7779
If no assertions fail, call `pass()` with the input value (usually `act$val`). Returning the input value is good practice since expectation functions are called primarily for their side-effects (triggering a failure). This allows expectations to be chained:

0 commit comments

Comments
 (0)