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

Fake is callable even if not declared callable #9

Open
quantoid opened this issue Feb 28, 2019 · 3 comments
Open

Fake is callable even if not declared callable #9

quantoid opened this issue Feb 28, 2019 · 3 comments

Comments

@quantoid
Copy link

quantoid commented Feb 28, 2019

When rendering templates in Django the template engine will use callable() to decide whether to call a variable to resolve a value. When testing using a Fake object this results in an exception:

RuntimeError: fake:StatusPage object cannot be called (maybe you want Fake.is_callable() ?)

This is because Fake.__call__ is declared even though the fake is not expected to be called, so callable(fake) is always True and the template engine will try to call it.

Took me a while to realise this and come up with a workaround, which is to make sure the fake is callable and just returns itself so the template engine resolves it to itself:

my_fake.expects_call().returns(my_fake)

I'm not sure this is a bug, but I think it's something worth highlighting in the documentation. The documentation doesn't seem to be searchable, but I had a skim through and couldn't find a reference to callable().

@quantoid
Copy link
Author

This would be a good place to mention the callable() gotcha: https://fudge.readthedocs.io/en/latest/api/fudge.html#fudge.Fake.is_callable

@quantoid
Copy link
Author

@jwhitlock
Copy link

If the object has an attribute do_not_call_in_templates, then Django doesn't try to call it:

https://github.com/django/django/blob/97e637a87fb45c4de970cca6cb783d93473c9d15/django/template/base.py#L851-L853

This path was added when Django converted User.is_authenticated from a method to a boolean. Maybe this can help make a callable Fake that Django doesn't call in templates.

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

2 participants