模拟 python 库或包装方法

在我的代码release_bundler.py文件中


class MavenDependenciesPathsBuilderStrategy(PathsBuilderStrategy):


  def build_bundler_copy_paths(self, mapping_values):

    source = join(getcwd(),'target','dependency',mapping_values[0])

    destination = join(getcwd(),'target','generated-resources',mapping_values[1],mapping_values[0])

    return [source, destination]


class NestedModulePathsFilterStrategy(FilterStrategy):


  def filter_changeset_paths(self, changeset_paths, bundling_map, paths_builder_strategy):

    for mapping_key in bundling_map.keys():

      if(mapping_key in changeset_paths):

        mapping_values = bundling_map.get(mapping_key).values()

        copy_paths = paths_builder_strategy.build_bundler_copy_paths(mapping_values)

        return copy_paths

如果我想测试该filter_changeset_paths方法,我必须在getcwd方法内模拟这两种方法,build_bundler_copy_paths还是只模拟后者呢?


我尝试在我的测试中模拟该方法release_bundler_test.py,导入如下分类:


from release_bundler import NestedModulePathsFilterStrategy, MavenDependenciesPathsBuilderStrategy


然后修补MavenDependenciesPathsBuilderStrategy课程


def mock_build_bundler_copy_paths(self, mapping_values):

  return ['/cwd/foo','/cwd/bar']



但我不认为这个模拟有效,因为self.assertIsNotNone(result)失败


所以问题是:


我嘲笑的方式正确吗?无法理解

会嘲笑该MavenDependenciesPathsBuilderStrategy方法还是我也必须嘲笑os.getcwd它的内部方法?


FFIVE
浏览 154回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python