Ensure mocked User.get_profile is removed by @ensure_user_profile
Review Request #8869 — Created April 3, 2017 and submitted
The
@ensure_user_profile
decorator ensures thatUser.get_profile
exists because it has been removed in Django 1.7+. For compatability,
some functionality (and therefore unit tests) require it to exist
(i.e., be on Django 1.6 or have it added by a third party).One issue is that when using this in conjunction with KGB to replace the
definition ofget_profile
that the profile will not be deleted, as
SpyAgency.tear_down
will execute afterrequire_user_profile
has
finished, which results in the original method not being deleted.
Furthermore, the mock method we add will stick around as
SpyAgency.tear_down
restores it.To combat this issue, we now check to see if the function has been
replaced by aFunctionSpy
and unspy on it if that is the case. That
will result inSpyAgency
not modifying theUser
object during
tear_down
.Additionally, we were comparing
User.get_profile
to our mock method,
which will never be equal becauseUser.get_profile
will be an unbound
method and not the original function. We now compare to the underlying
function instead.
Ran unit tests on Django 1.6 and 1.8.
- Description:
-
The
@ensure_user_profile
decorator ensures thatUser.get_profile
exists because it has been removed in Django 1.7+. For compatability, ~ some unit tests require it to exist (i.e., be on Django 1.6 or have it ~ added by a third party). ~ some functionality (and therefore unit tests) require it to exist ~ (i.e., be on Django 1.6 or have it added by a third party). One issue is that when using this in conjunction with KGB to replace the
definition of get_profile
that the profile will not be deleted, asSpyAgency.tear_down
will execute afterrequire_user_profile
hasfinished, which results in the original method not being deleted. Furthermore, the mock method we add will stick around as SpyAgency.tear_down
restores it.To combat this issue, we now check to see if the function has been
replaced by a FunctionSpy
and unspy on it if that is the case. Thatwill result in SpyAgency
not modifying theUser
object duringtear_down
.Additionally, we were comparing
User.get_profile
to our mock method,which will never be equal because User.get_profile
will be an unboundmethod and not the original function. We now compare to the underlying function instead.