1

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, (I have edited according to Waldi's answer)

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

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 ./UnitTest/Data/Expected_Output_2"
Actual message: "cannot open the connection"
In addition: Warning message:
In open.connection(con, "rb") :
  cannot open file './UnitTest/Data/Expected_Output_2': Permission denied
Luiy_coder
  • 321
  • 2
  • 11

2 Answers2

1

You just have to test exactly the expected error message :

library(testthat)


LoadData = function(inputPath) {
  if(length(list.files(inputPath))==0){
    stop(paste0("There's no file at ", inputPath))
  }
}


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

Created on 2020-07-06 by the reprex package (v0.3.0)

The above test succeeded because the result of :

    LoadData("./UnitTest/Data/Expected_Output2")

is following error :

Error in LoadData("./UnitTest/Data/Expected_Output2") : 
  There's no file at ./UnitTest/Data/Expected_Output2
Waldi
  • 39,242
  • 6
  • 30
  • 78
  • Now am getting an error like this ``` Error: `LoadData(filePath)` threw an error with unexpected message. Expected match: "There's no file at :filepath" Actual message: "argument \"inputPath\" is missing, with no default"``` . – Luiy_coder Jul 06 '20 at 14:08
  • I don't understand the error message you get as a filepath is supplied as InputPath argument of LoadData. Did you run exactly the example I provided? – Waldi Jul 06 '20 at 14:39
  • Yes I did the same. – Luiy_coder Jul 06 '20 at 14:41
  • I now generated/edited the code above with Reprex so hat I'm 100% sure it doesn't throw an error on my system. can you copy it in a new script and let it run? If you still have an error, it could be linked to operating system : on which system are you working? – Waldi Jul 06 '20 at 14:48
  • I have used the same piece of your code. It is now throwing me another error. I think it has something to do with operating system. – Luiy_coder Jul 06 '20 at 15:23
  • I'm not sure [file.exist is the right command](https://stackoverflow.com/a/46147902/13513328) because what I undesrtand is that you're looking if there's a file in the directory. So I made a modification to LoadData : please tell me if it works as expected – Waldi Jul 06 '20 at 16:03
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/217325/discussion-between-luiy-coder-and-waldi). – Luiy_coder Jul 06 '20 at 16:09
  • The code which you have shared works perfectly in non work enviroment. The RStudio I'm working with is work environment and it needs elevated access rights. That is why I'm receiving this error. I have figured it out just now. – Luiy_coder Jul 06 '20 at 16:09
0

One way to fix this was to assign a json file which actually doesn't exist in the directory like this:

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

Here we need to be careful to understand that earlier we were just assigning a path which was empty. But we need to assign a hypothetical file which actually doesn't exist. This worked for me. My test succeed.

Luiy_coder
  • 321
  • 2
  • 11