Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

testing etc #3

Open
MilesMcBain opened this issue Jul 2, 2020 · 1 comment
Open

testing etc #3

MilesMcBain opened this issue Jul 2, 2020 · 1 comment

Comments

@MilesMcBain
Copy link
Collaborator

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)

@MilesMcBain
Copy link
Collaborator Author

Some more relevant discoveries:

  • assignInNamespace()
  • testthat::with_mock()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant