0
我试图做到的是嘲笑一个分裂的方法中这样模拟一个字符串的属性
def method_separator(s):
try:
a, b, c = s.split('-')
print a, b, c
except: ValueError:
print "An error occurred or so"
在测试
with self.assertRaises(ValueError):
with mock.patch(What to put here?) as mock_patch:
mock_patch.return_value = "TEXT" # To raise ValueError
this = method_separator('Some-good-text') # The mock should not let this good test to pass
我所做的模拟与string.split工作
def method_separator(s):
try:
a, b, c = split.string(s, '-')
print a, b, c
except: ValueError:
print "An error occurred or so"
with self.assertRaises(ValueError):
with mock.patch('string.split') as mock_patch:
mock_patch.return_value = "TEXT" # To raise ValueError
this = method_separator('Some-good-text') # The mock should not let this good test to pass
即使作品,问题依然存在,它是可以做的吗?模拟的结果“”.split(' - ')
你为什么需要*模拟*那个?!只需传入一个字符串,将其拆分为适当的值。 – jonrsharpe
那个方法只有一个片段,想法是嘲弄那个特定的部分,这就是为什么我把它放在一个方法来看看如何。在字符串来自我不能操作的方法的主要方法。 – miphos
然后你模拟这些方法,返回一个合适的字符串。没有一个实际的代表性例子,很难提出进一步的建议。 – jonrsharpe