Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Making a pure mock iterable #74

Closed
brycecovert opened this Issue Apr 3, 2012 · 3 comments

Comments

Projects
None yet
2 participants

I saw an old issue (#11) that was fixed a while ago. It looks like it works great on partial mock of classes, but not great on pure mocks, or partial mocking an instance. Both of these fail:

  def test_flexmock_should_mock_private_class_methods(self):
    class Foo:
      def __iter__(self): pass

    flexmock(Foo()).should_receive('__iter__').and_yield(1, 2, 3)
    assertEqual([1, 2, 3], [x for x in Foo()])

  def test_should_allow_special_methods_on_pure_mocks(self):
    foo = flexmock().should_receive('__iter__').and_yield(1, 2, 3).mock
    assertEqual([1, 2, 3], [x for x in foo])

It's not immediately obvious what the best direction is here. Should _setattr assign on the class, if it's there?

I'm happy to submit a patch for this, but I want to make sure I have the right solution.

@has207 has207 closed this in 24b3fd9 Apr 3, 2012

Owner

has207 commented Apr 3, 2012

I added iter support to the Mock class to enable should_yield to work on fake objects.

It should already work fine for classes, your example above partially mocks an instance rather than the class:

flexmock(Foo()).should_receive('iter').and_yield(1, 2, 3)

What you want is:

flexmock(Foo).should_receive('iter').and_yield(1, 2, 3)

@has207 has207 reopened this Apr 3, 2012

Owner

has207 commented Apr 3, 2012

Blah, I see what you were saying about instances now, though your example is still broken since you were using two different instances. The patch I submitted fixes things on python 2.x but it seems like iter on the class takes precedence on python3.x even when one exists on the instance. Looks like this will take a bit more work (though if you only care about 2.x then it should be working now).

Owner

has207 commented Apr 3, 2012

Actually, it looks like it's not just python3.x but any new-style objects.

@has207 has207 closed this in 1dd78df Apr 15, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment