nealrichardson / httptest

A Test Environment for HTTP Requests in R
https://enpiar.com/r/httptest/
Other
79 stars 10 forks source link

use with_mock_api without hashed filename #26

Closed jcpsantiago closed 4 years ago

jcpsantiago commented 4 years ago

I need to do POST requests which include fields that change over time like timestamp, r version, package versions etc. The current logic of hashing body prevents me from using with_mock_api because it never finds the correct mock file. Is there already a way of bypassing this? Looking at the source code there seems to be no option for it.

nealrichardson commented 4 years ago

I see a couple of options, not mutually exclusive:

pr130 commented 4 years ago

@jcpsantiago you've probably solved this already but leaving it here for folks looking for a solution (I just ran into this problem). I used the mocking approach successfully and stubbbed out Sys.time using the mockery package. The key was to set the depth argument as well because of the nested nature of my functions (could be different for you).

with_mock_api({
    test_that("success generates message", {
        time_stub <- "2020-03-14 12:51:02 CET"
        # depth = 3 because gen_action_ is called further down
        mockery::stub(gen_action_, "Sys.time", time_stub, depth = 3)
        expect_message(
            myfunction(...)
        )
    })
})