Plugs and Layouts in R

I spent a lot of time with RSpec and some time using xunit style tests. I got used to the fact that at my disposal were mockery and wrapping.

Is there something like mocks or stubbing in R? What is their scope?

Should you not isolate your tests?

+6
source share
3 answers

It was not available until 4 years after you asked this question, but it looks like testthat now includes the with_mock() function:

https://github.com/hadley/testthat/blob/master/R/mock.R

It seems to be very well suited for counting.

+4
source

There are CRAN RUnit , svUnit, and testthat packages , all of which provide unit testing for R.

If you need another structure, you may have to write it. CRAN will gladly accept your code (if it meets the usual standards).

+4
source

There is also a stub function in the mockery package. It is similar to with_mock , but it also allows you to cut primitives and functions from the base packages of R.

Example:

 g = function(y) y f = function(x) g(x) + 1 test_that('demonstrate stubbing', { # before stubbing expect_equal(f(1), 2) # replace the function 'g' when called from 'f' stub(f, 'g', function(...) 100) expect_equal(f(1), 101) }) 
+1
source

Source: https://habr.com/ru/post/910754/


All Articles