2017-09-29 17 views
1

このテストに合格しないのはなぜですか?エラーメッセージのかっこが原因でexpect_errorテストが失敗する

my_fun <- function(x){ 
    if(x > 1){stop("my_fun() must be called on values of x less than or equal to 1")} 
    x 
} 

library(testthat) 
expect_error(my_fun(2), 
      "my_fun() must be called on values of x less than or equal to 1") 

それはエラーメッセージを返します。

Error: error$message does not match "my_fun() must be called on values of x less than or equal to 1". Actual value: "my_fun() must be called on values of x less than or equal to 1"

あなたは機能とテストの両方から()を削除した場合、テストが、それは括弧についての何かだと思うように私をリードし、パスを行います。

答えて

3

expect_errorには、文字列だけでなく、正規表現も渡しています。括弧は正規表現の特殊文字であり、エスケープする必要があります。 (カッコは正規表現でのグループ化に使用されます)。括弧を扱う、すぐ下にexpect_errorを変更するには、一般的に

expect_error(my_fun(2), 
      "my_fun\\(\\) must be called on values of x less than or equal to 1") 

以上、完全一致などの文字列をテストするためにfixed = TRUEを指定:

expect_error(my_fun(2), 
      "my_fun() must be called on values of x less than or equal to 1", 
      fixed = TRUE) 
関連する問題