Skip to content

testing etc #3

@MilesMcBain

Description

@MilesMcBain

I was thinking about how we could test and I thought about the idea of mocking curl_fetch_memory.

Check this out:

curl_env <- environment(curl::curl_fetch_memory)
old_curl_fetch_memory <- curl::curl_fetch_memory
unlockBinding(quote(curl_fetch_memory), curl_env)
assign("curl_fetch_memory",
       function(...) list(
                       response_code = 200,
                       content = charToRaw("Hello Curled!")),
       envir = curl_env)
a <- curl::curl_fetch_memory("http://whatever")
print(a)
#> $response_code
#> [1] 200
#> 
#> $content
#>  [1] 48 65 6c 6c 6f 20 43 75 72 6c 65 64 21
print(rawToChar(a$content))
#> [1] "Hello Curled!"

assign("curl_fetch_memory",
       old_curl_fetch_memory,
       envir = curl_env)

lockBinding(quote(curl_fetch_memory),
            curl_env)

a <- curl::curl_fetch_memory("http://whatever")
#> Error in curl::curl_fetch_memory("http://whatever"): Could not resolve host: whatever

Created on 2020-07-02 by the reprex package (v0.3.0)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions