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

Double-colon mocking not working at depth #52

Closed
kenahoo opened this issue Sep 17, 2021 · 5 comments
Closed

Double-colon mocking not working at depth #52

kenahoo opened this issue Sep 17, 2021 · 5 comments

Comments

@kenahoo
Copy link

kenahoo commented Sep 17, 2021

Here's my reprex, showing a failed attempt at mocking:

library(mockery)


func <- function() {
  .request("http://google.com")
}

.request <- function(url) {
  RCurl::getURL(url)
}

mock_response = mock("foo")
stub(func, 'RCurl::getURL', mock_response, depth=5)
print(func())
#> [1] "<HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">\n<TITLE>301 Moved</TITLE></HEAD><BODY>\n<H1>301 Moved</H1>\nThe document has moved\n<A HREF=\"http://www.google.com/\">here</A>.\r\n</BODY></HTML>\r\n"

Created on 2021-09-17 by the reprex package (v2.0.1)

If I change to mocking an imported function instead, it works as desired:

library(mockery)
library(RCurl)

func <- function() {
  .request("http://google.com")
}

.request <- function(url) {
  getURL(url)
}

mock_response = mock("foo")
stub(func, 'getURL', mock_response, depth=5)
print(func())
#> [1] "foo"

Alternatively, if I only need to go 1 level deep, and I remove the depth=5 argument, it also works as desired:

library(mockery)

func <- function() {
  RCurl::getURL("http://google.com")
}

mock_response = mock("foo")
stub(func, 'RCurl::getURL', mock_response)
print(func())
#> [1] "foo"

If the depth=5 argument is included (which theoretically should have no effect here), it fails:

library(mockery)

func <- function() {
  RCurl::getURL("http://google.com")
}

mock_response = mock("foo")
stub(func, 'RCurl::getURL', mock_response, depth=5)
print(func())
#> [1] "<HTML><HEAD><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\">\n<TITLE>301 Moved</TITLE></HEAD><BODY>\n<H1>301 Moved</H1>\nThe document has moved\n<A HREF=\"http://www.google.com/\">here</A>.\r\n</BODY></HTML>\r\n"
@jimhester
Copy link
Member

Seems to be a duplicate of #29?

@kenahoo
Copy link
Author

kenahoo commented Sep 17, 2021

I should mention: neither of those workarounds are great, because they both require changing the code that's being tested. I'll probably go with the don't-use-double-colon workaround for now, though, because changing the structure would be quite a bit more invasive than that.

@kenahoo
Copy link
Author

kenahoo commented Sep 17, 2021

@jimhester hmm, yes, it might be the same bug - somehow when I was debugging this yesterday, I saw #29 and managed to convince myself that's not what was happening, but right now it seems to be the same.

The other angle that comes to play here is whether or not the code being tested is in another namespace/package, and yesterday I thought that mattered, but today it seems to be the same bug in either case, would you agree there?

@jimhester
Copy link
Member

Yeah, from just looking at the examples I think it looks to be the same issue.

@hadley
Copy link
Member

hadley commented Oct 31, 2023

We now recommend that you use testthat::local_mocked_bindings() instead of mockery. It doesn't fully solve the mocking of namespaced functions problem, but the docs suggest a number of reasonably convenient workarounds.

@hadley hadley closed this as completed Oct 31, 2023
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

3 participants