I am writing a unit test in which I test if the location of a gef on a map is drawn correctly. In order to do this I am trying to mock a gef entity using the code shown below
When I try to run my unit test I get the following error:
Can somebody help me with this?
mock_params function expects an instance of a
Parametrization. It seems
GEFfileDatabaseController.parametrization is a class and not an instance. You can easily fix this by using
GEFfileDatabaseController.parametrization() (with parentheses) instead.
It makes sense that we also allow for the
Parametrization class in addition to the instance in
mock_params, to prevent this tricky bug in the future.
Thanks for your response. Your reasoning makes sense and I will add the parentheses to see if it works. May I suggest a correction to be made on the documentation on mock_params in this case? The screenshot below is directly taken from the docs and does not use parentheses. Also in this screenshot there is no comma after MyEntityController.parametrization which leads to a syntax error.
Also, when looking at the documentation on MockedEntity I can see that there are also no parentheses (the comma is present in this case).
Now that we are on this topic can I ask you what the integers in the dictionaries for entities and file_resources are used for? In the mock_params documentation they are set to 2 and 3 respectively but in the MockedEntity documentation both integers are set to 1. Right now I do not know what the integers are used for and therefore find the documentation hard to interpret.
The screenshot below is directly taken from the docs and does not use parentheses.
The example in the docs is correct if (and only if) the parametrization on the controller is defined with parentheses (
parametrization = Parametrization()), but is indeed incorrect if no parentheses were used. Allowing for also the class in addition to the instance would resolve this inconsistency.
Also in this screenshot there is no comma after MyEntityController.parametrization which leads to a syntax error.
Good find. We will update the docs.
Now that we are on this topic can I ask you what the integers in the dictionaries for entities and file_resources are used for?
A value saved on a
FileField stores an
int in the params that refers to the file-resource id. Similar, entity fields store an
int that refers to the entity id. As the docs state, the
file_resources dict “maps FileResource source id in the params to the mocked file resource”. Similar, the
entities dict “maps entity id in the params to the mocked entity”.
By using a dict, you have maximum flexibility over mocking your params. For example, you can have entity with id 1 have a different mock than entity with id 2.
That’s very clear, I hadn’t thought about using parentheses in the controller.
Thanks for your explanation and your help!