Skip to content

Fake is callable even if not declared callable #9

@quantoid

Description

@quantoid

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().

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions