dimfalk / netatmo.weather

R wrapper for Netatmo Weather API
GNU General Public License v3.0
3 stars 0 forks source link

`test-*()`: mock API to increase code coverage #49

Open dimfalk opened 2 years ago

dimfalk commented 2 years ago

https://github.com/ropensci/vcr

https://github.com/ropensci/webmockr/

https://github.com/r-lib/mockery

https://github.com/nealrichardson/httptest

https://github.com/nealrichardson/httptest2

dimfalk commented 7 months ago
 library(mockery)

# set up the mock
mock_download_file <- mock_function(return_value = TRUE)
mock_download_file(file = "test.txt", url = "http://example.com/test.txt")

# call the function that uses download.file
your_function_to_test()

# check the output
expect_true(mock_download_file.calls$file[1] == "test.txt")
expect_true(mock_download_file.calls$url[1] == "http://example.com/test.txt")
library(mockery)

mocked_response <- list(status = 200, body = "Mocked response")

# set up the mock
mock_api_call <- mockery::mock_function(return_value = mocked_response)

# make the request
response <- mock_api_call()

# check the response
response