lundi 6 juillet 2020

how to write expect_error in testthat to throw an error if there is no file in the directory?

I'm running a R code using testthat to throw an error if there is no file in the directory. My test code is as follows,

test_that(desc = "Test for 'LoadData' Condition 1",
          code = {
            filePath = "./UnitTest/Data/Expected_Output2"
            expect_error(LoadData(inputPath = filePath),"There's no file at location")
          }
)

My function is,

LoadData = function(inputPath) {
if(!file.exists(inputPath){
 stop(paste0("There's no file at ", inputPath))
   }
}

My test code fails with this message,

Error: `LoadData(inputPath = filePath)` threw an error with unexpected message.
Expected match: "There's no file at location"
Actual message: "There's no file at ./UnitTest/Data/Expected_Output2"

Is there any fix for this?

Aucun commentaire:

Enregistrer un commentaire