samedi 2 mai 2015

Generator returning Mocks that fail assert_called_with

I'm trying to test a script that is organized like this:

class Processor(object):
    def __init__(self, *args):
        pass

def my_iterator(elements):
    for element in elements:
        yield Processor(element)

I just want to test that a Processor object was created for each element in the iterable that was passed to my_iterator().

I'm testing Processor separately, so I wrote a test for my_iterator() that looks like this:

import mock

from mymodule import my_iterator

class TestMyIterator(object):

    @mock.patch('mymodule.Processor')
    def test_my_iterator(self, ProcessorMock):
        vals = range(3)
        g = my_iterator(vals)

        for idx, item in enumerate(g):
            item.assert_called_with(vals[idx])

But when I run the test (using nosetests), it tells me that item was NotCalled at all. Shouldn't it have been called when my_iterator() yielded an element?

Aucun commentaire:

Enregistrer un commentaire