BroVic
BroVic

Reputation: 1079

R: How does one use the expect_success function for unit tests?

I have an expression that I want to test using the testthat package. In the documentation for expect_success it states usage of the function to be

expect_success(expr)

where expr is an expression that evaluates to a single expectation.

For instance, with this code

test_that("Expectation succeeds", {
  x <- 1:10
  expect_success(mean(x)) 
})

I get the error

Error: Test failed: 'Expectation succeeds'
* no expectation used.

Where am I going wrong?

Upvotes: 2

Views: 505

Answers (1)

BroVic
BroVic

Reputation: 1079

Actually, while I was writing the question, I further experimented with the code and found that I didn't at first fully understand the documentation. This function is used to test other expectations. So, for the example used in the question, this works as expected

test_that("Expectation succeeds", {
  x <- 1:10
  expect_success(expect_type(mean(x), 'double')) 
})

Upvotes: 5

Related Questions