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

1

16

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

Aphid answered 29/4, 2018 at 4:56 Comment(6)
Could you post the code you are running that throws the error?Adversative
I am using quite a lot of files, so it was easier to reproduce the error in a new, clean project. I edited the description.Aphid
I can't reproduce this on my macbook unfortunately.Adversative
I have the same issue. Did you solve your problem? (A colleague runs the same code without this problem!)Quadrireme
See my edit to the answer below...Quadrireme
I use version:2.0.1 is working fine with usethis::use_testthat() and usethis::use_test("foo") but context("test-foo") instead of source("bar.R") `Unguent
D
21

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).

Dial answered 18/5, 2018 at 2:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.