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

Add ChaiBase.mock_of(interface) to create mock objects that have isinstance(mock, interface) = True #28

Closed
wants to merge 4 commits into from

Conversation

tim-mitchell
Copy link

The use of isinstance checks on parameter values is quite common when using the abc module or PyContracts. The mock_of() method returns a Mock() instance that passes the isinstance test.

The interface class passed to mock_of must have a meta-class so that we can override the instancecheck method on it. This is always the case with interface classes created with the abc module or PyContracts.

@awestendorf
Copy link
Member

@tim-mitchell looks good. please clear up the Travis errors, squash the commits and I'll merge this in

@tim-mitchell
Copy link
Author

Hi Aaron,

Sorry, but I don’t know how to do either of those things. I have added you
as a collaborator on tim-mitchell/chai, would you mind making the
appropriate changes please.

Thanks

Tim

Tim Mitchell

Senior Software Engineer

phone: +64 3 961 1031 ext. 217 <+64-3-961-1031;ext=217>

email: tim.mitchell@leapfrog3d.com

skype: tim.mitchell.leapfrog3d

address: 41 Leslie Hills Drive, Riccarton, Christchurch 8011, New Zealand

www.leapfrog3d.com | www.aranzgeo.com

From: Aaron Westendorf [mailto:notifications@github.com]
Sent: Wednesday, 8 July 2015 6:12 a.m.
To: agoragames/chai
Cc: Tim Mitchell
Subject: Re: [chai] Add ChaiBase.mock_of(interface) to create mock
objects that have isinstance(mock, interface) = True (#28)

@tim-mitchell https://github.com/tim-mitchell looks good. please clear up
the Travis errors, squash the commits and I'll merge this in


Reply to this email directly or view it on GitHub
#28 (comment).

@awestendorf
Copy link
Member

@tim-mitchell I tried every which way to push a requirements change to your master branch, kept getting errors from git complaining that I was trying to push an out-of-date branch even though I pulled down all your changes.

Speaking of which, I found several more changes in your master since this PR was opened. To keep the feature development separate from any other changes you may have, and the commit I pushed, I added the branch https://github.com/tim-mitchell/chai/tree/mock_of.

I tried using the feature on datetimes though and it didn't work. I called:

self.mock_of(datetime.datetime, obj=module_of_my_model, attr='datetime')

and saw the following two errors

    self.mock_of(datetime.datetime, obj=module_of_my_model, attr='datetime')
  File "build/bdist.linux-x86_64/egg/chai/chai.py", line 270, in mock_of
    meta_cls.__instancecheck__ = _instance_check
TypeError: can't set attributes of built-in/extension type 'type'

    super(MyTestCase, self).tearDown()
  File "build/bdist.linux-x86_64/egg/chai/chai.py", line 197, in tearDown
    setattr(mock[0], mock[1], mock[2])
TypeError: can't set attributes of built-in/extension type 'type'

On second glance through the API, I'm also wondering why we need mock_of, when we could add another kwarg parameter to mock that would suite the same purpose.

@tim-mitchell
Copy link
Author

@awestendorf The changes since the pull request are fixes to get mock_of to work correctly on python 3. I am not doing anything else on this fork. I could not get travis to work for me to test on pypy.

mock_of() is intended to work with interfaces created with the abc module, not with types such as datetime. I don't know that you can override the isinstance check of types in general. Still, a better error message would be good in this situation.

No particular reason for separate mock_of method, am happy to fold it into mock if you would prefer.

@czarneckid czarneckid closed this Mar 11, 2021
@czarneckid czarneckid deleted the branch agoragames:master March 11, 2021 16:46
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

Successfully merging this pull request may close these issues.

3 participants