在我的代码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']
@mock.patch('release_bundler.MavenDependenciesPathsBuilderStrategy', 'build_bundler_copy_paths', mock_build_bundler_copy_paths)
def test_nested_module_filter_changeset_paths(self):
pc_maps = {'ProcessingComponents/ProcessContainer':['ProcessContainerInstaller-bin.zip','SERVICES/Installer'],'ProcessingComponents/DataGrid':['ProcessContainerInstaller-bin.zip','SERVICES/Installer']}
changed_paths = ['ProcessingComponents/ProcessContainer/ProcessContainerRuntime/main/java/com/suntecgroup/tbms/container/ContainerException.java']
filter_test = NestedModulePathsFilterStrategy()
result = filter_test.filter_changeset_paths(changed_paths,pc_maps, MavenDependenciesPathsBuilderStrategy())
self.assertIsNotNone(result)
self.assertEquals(result[0], '/cwd/foo')
self.assertEquals(result[0], '/cwd/bar')
但我认为此模拟无效,因为self.assertIsNotNone(result)
失败
所以问题是:
MavenDependenciesPathsBuilderStrategy
方法上进行模拟还是会在方法内部进行os.getcwd
的模拟?答案 0 :(得分:0)
糟糕的是,我的新手python大脑花了一些时间来掌握一些东西:
这是很好的方式
@mock.patch('release_bundler.MavenDependenciesPathsBuilderStrategy.build_bundler_copy_paths')
def test_filters_pc_from_changeset_paths(self, mock_build_bundler_copy_paths):
pc_maps = {'ProcessingComponents/ProcessContainer':['ProcessContainerInstaller-bin.zip','SERVICES/Installer']}
changed_paths = ['ProcessingComponents/ProcessContainer/ProcessContainerRuntime/main/java/com/suntecgroup/tbms/container/ContainerException.java']
with mock.patch('release_bundler.NestedModulePathsFilterStrategy.do_copy') as mock_do_copy:
mock_do_copy.return_value = ''
filter_test = NestedModulePathsFilterStrategy()
filter_test.filter_changeset_paths(changed_paths,pc_maps, MavenDependenciesPathsBuilderStrategy())
mock_build_bundler_copy_paths.assert_called_with(['ProcessContainerInstaller-bin.zip','SERVICES/Installer'])
mock_do_copy.assert_called()
我可能是错的,但是我认为我们不能将多个装饰器应用于一个方法?