Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in x[[method]](...) : attempt to apply non-function in testthat test when sourcing file

Tags:

r

testthat

I am developing an R app and came up with a workaround to integrate testthat in it (it usually requires your project to be a package) by adding a DESCRIPTION file to the project root.

I got this method from a colleague who managed to get it to work like this.

The problem is, when I try to test anything (even empty test files), I get this error:

Error in x[[method]](...) : attempt to apply non-function
Calls: <Anonymous> ... <Anonymous> -> o_apply -> lapply -> FUN -> <Anonymous>
Execution halted

What I know is that this appears only on macOS. My colleague can still run this app without problems on Windows.

What's the deal with it?

The MCVE for reproducing this error would be:

  • create a minimum DESCRIPTION file with content:

    Package: testpckg
    
  • add testthat to your project:

    usethis::use_testthat()
    usethis::use_test("foo")
    
  • create empty file bar.R
  • in test-foo.R, source bar: source("bar.R")

RStudio Version 1.1.447, R version 3.4.4, Mac OS X 10_13_4

like image 434
Teodor Ciuraru Avatar asked Apr 29 '18 04:04

Teodor Ciuraru


1 Answers

This appears to be due to a bug in testthat 2.0.0. Adding a context("Name of test context") to the top of each test file worked for me (tested using Ubuntu and Windows).

like image 77
nokome Avatar answered Nov 04 '22 23:11

nokome