Ensure mocked User.get_profile is removed by @ensure_user_profile
Review Request #8869 — Created April 3, 2017 and submitted
The
@ensure_user_profiledecorator 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_profilethat the profile will not be deleted, as
SpyAgency.tear_downwill execute afterrequire_user_profilehas
finished, which results in the original method not being deleted.
Furthermore, the mock method we add will stick around as
SpyAgency.tear_downrestores it.To combat this issue, we now check to see if the function has been
replaced by aFunctionSpyand unspy on it if that is the case. That
will result inSpyAgencynot modifying theUserobject during
tear_down.Additionally, we were comparing
User.get_profileto our mock method,
which will never be equal becauseUser.get_profilewill 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_profiledecorator ensures thatUser.get_profileexists 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_profilethat the profile will not be deleted, asSpyAgency.tear_downwill execute afterrequire_user_profilehasfinished, which results in the original method not being deleted. Furthermore, the mock method we add will stick around as SpyAgency.tear_downrestores it.To combat this issue, we now check to see if the function has been
replaced by a FunctionSpyand unspy on it if that is the case. Thatwill result in SpyAgencynot modifying theUserobject duringtear_down.Additionally, we were comparing
User.get_profileto our mock method,which will never be equal because User.get_profilewill be an unboundmethod and not the original function. We now compare to the underlying function instead.