samedi 2 mai 2015

Python flexmock Best Practice Mocking shutil.copy Inside Another Method

I managed to solve the original problem I was having by finding this solution: Mocking functions with FlexMock in Python?

More specifically, my test now looks like this:

def test_process_skips_if_file_does_not_exist(self):
    # given a sample config is provided
    self.bp.configs = [{'config_path': "DOES_NOT_EXIST"}]
    # make sure copy is never called
    flexmock(sys.modules['libs.backup']).should_receive('copy').never()

    self.bp.local_backup()

The question I have concerning flexmock, is whether what I am doing would be considered a good utilization/best practice of flexmock?

I am able to do this also using mock.patch. Pretty much the implementation that is provided here:

How to get the call count using Mock @patch?

My question is whether I am taking the right approach in flexmock with how I am trying to test my method.

Here is a snippet of the code I am trying to test:

    def local_backup(self):
        for data in self.configs:
            try:
                if exists(data.get('config_path')):
                    copy(data.get('config_path'), self.backup_location)
            except IOError:
                raise IOError

Thank you.

Aucun commentaire:

Enregistrer un commentaire