Testthat: Confusing result with expect_error()

Created on 7 Mar 2018  路  3Comments  路  Source: r-lib/testthat

Hi,
I obtained this confusing result when trying to use the expect_error() function as follows:

> expect_error(build_report(9), "is.character(file) is not TRUE")
Error: `build_report(9)` threw an error with unexpected message.
Expected match: "is.character(file) is not TRUE"
Actual message: "is.character(file) is not TRUE"

The function I'm testing has a quick validation check that looks like this

...
stopifnot(is.character(file))
...

To make sure that it wasn't my vision playing tricks on me, I copied both expectations from the output and pasted them into identical() and sure enough the result was TRUE.

Have I gone wrong somewhere or is this a bug?

Regards.

Most helpful comment

Quick guess: the matching string is a regular expression. Do

expect_error(build_report(9), "is.character(file) is not TRUE", fixed = TRUE)

to avoid having to write regexes.

All 3 comments

Quick guess: the matching string is a regular expression. Do

expect_error(build_report(9), "is.character(file) is not TRUE", fixed = TRUE)

to avoid having to write regexes.

@gaborcsardi Yes, that did it. Thanks!

I kept getting bit by this. Maybe the error message can be improved.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hannaET picture hannaET  路  7Comments

Christoph999 picture Christoph999  路  4Comments

krlmlr picture krlmlr  路  3Comments

hadley picture hadley  路  5Comments

mjsteinbaugh picture mjsteinbaugh  路  7Comments