krlmlr / mockr

Drop-in replacement for testthat::with_mock()

Home Page:https://krlmlr.github.io/mockr/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

mockr package

pec63 opened this issue · comments

commented

I am using testthat to test my package.
I need to mock a function so I started to use with_mock.
First I tried mockr::with_mock in this way:
testthat(..., {..mockr::with_mock(...) }) this works without warnings or errors when I test the package; but an error occurs when I check it.

W checking for unstated dependencies in 'tests' (4.1s)
'::' or ':::' import not declared from: 'mockr'

  • checking tests (1.3s)

  • Running 'testthat.R' [174s] (2m 53.8s)
    E Some test files failed
    Running the tests in 'tests/testthat.R' failed.
    Last 13 lines of output:

    test_check("Par")
    == Failed tests ================================================================
    -- Error (test-cProfPar.R:92:13): calcolo profitti dinamici in base a te -------
    <packageNotFoundError/error/condition>
    Error in loadNamespace(x): there is no package called 'mockr'
    Backtrace:
    x

    1. -base::loadNamespace(x) test-cProfPar.R:92:12
    2. -base::withRestarts(stop(cond), retry_loadNamespace = function() NULL)
    3. \-base:::withOneRestart(expr, restarts[[1L]])
      
    4.   \-base:::doWithOneRestart(return(expr), restart)
      

    [ FAIL 1 | WARN 0 | SKIP 0 | PASS 97 ]
    Error: Test failures
    Execution halted
    v checking for non-standard things in the check directory
    v checking for detritus in the temp directory

    See
    'C:/Users/pcaro/Documents/Lavoro/Cva/Elaborati/201906-08VarPaR/PaR/SwFactory/ReleaseSvilPaR/PostPackage/Par.Rcheck/00check.log'
    for details.

-- R CMD check results -------------------------------------------------------------------- Par 5.0.0 ----
Duration: 4m 2.5s

checking tests ...
See below...

checking for unstated dependencies in 'tests' ... WARNING
'::' or ':::' import not declared from: 'mockr'

-- Test failures --------------------------------------------------------------------------- testthat ----

library(testthat)
library(Par)

test_check("Par")
== Failed tests ================================================================
-- Error (test-cProfPar.R:92:13): calcolo profitti dinamici in base a te -------
<packageNotFoundError/error/condition>
Error in loadNamespace(x): there is no package called 'mockr'
Backtrace:
x

  1. -base::loadNamespace(x) test-cProfPar.R:92:12
  2. -base::withRestarts(stop(cond), retry_loadNamespace = function() NULL)
  3. \-base:::withOneRestart(expr, restarts[[1L]])
    
  4.   \-base:::doWithOneRestart(return(expr), restart)
    

[ FAIL 1 | WARN 0 | SKIP 0 | PASS 97 ]
Error: Test failures
Execution halted

1 error x | 1 warning x | 0 notes v
Errore: R CMD check found ERRORs
Esecuzione interrotta

Exited with status 1.

If I use testthat(..., {..with_mock(...).. }) I get warnings during package tests but everything is ok during check. The warnings are as follows:

i Loading Par
i Testing Par
v | F W S OK | Context
v | 9 | ausiliarie [0.1s]
v | 29 | calcVen [62.9s]
v | 1 | cCopCv [0.6s]
v | 2 | cFix [8.8s]
v | 3 10 | cProfPar [21.7s]

Warning (test-cProfPar.R:92:13): calcolo profitti dinamici in base a te
with_mock() was deprecated in the 3rd edition.
i Please use mockr or mockery packages instead
Backtrace:

  1. testthat::with_mock(...) test-cProfPar.R:92:12
  2. testthat:::edition_deprecate(3, "with_mock()", "Please use mockr or mockery packages instead")

Warning (test-cProfPar.R:116:13): calcolo profitti dinamici in base a te
with_mock() was deprecated in the 3rd edition.
i Please use mockr or mockery packages instead
Backtrace:

  1. testthat::with_mock(...) test-cProfPar.R:116:12
  2. testthat:::edition_deprecate(3, "with_mock()", "Please use mockr or mockery packages instead")

Warning (test-cProfPar.R:140:13): calcolo profitti dinamici in base a te
with_mock() was deprecated in the 3rd edition.
i Please use mockr or mockery packages instead
Backtrace:

  1. testthat::with_mock(...) test-cProfPar.R:140:12
  2. testthat:::edition_deprecate(3, "with_mock()", "Please use mockr or mockery packages instead")

v | 17 | gs [6.0s]
v | 25 | InParam [3.6s]
v | 6 | inPrGme [9.8s]
v | 1 | logga

== Results ===============================================================================================
Duration: 113.6 s

[ FAIL 0 | WARN 3 | SKIP 0 | PASS 100 ]

The question is: how and where shoud I declare that I want to use mockr in order to avoid these issues?
I have tried to put library(mockr) in testthat.R but it does not work. Then I have tried to put it as the first line of the test script but it does not work ither.
Considering that the testthat::with_mock is deprecated, which is the right way to use with_mock with testthat?

THANK YOU!!!

Thanks. Do you need to add mockr to "Suggests" in DESCRIPTION?

commented

I did it in Imports and it worked.

commented

Thank you.

Looks solved, please open a new issue if not.