1 回答
TA贡献1793条经验 获得超6个赞
首先,try块中的代码太多。唯一引发 a ValueError(您的错误消息准确解决)的是int第一行的调用。其次,不要input在你计划测试的代码中硬编码;相反,传递默认为的第二个参数input,但允许您提供用于测试的确定性函数。
def add_member(self, input=input):
p_name = input("Enter your project name: ")
participant_names = []
roles = []
while True:
try:
many = int(input("How many member do you want to add? "))
break
except ValueError:
print("Enter an integer")
for i in range(many):
name = input("Enter name: ")
role = input("Enter role: ")
participant_names.append(name)
roles.append(role)
self.notebook.add_member(p_name, participant_names, roles)
def make_input(stream):
def _(prompt):
return next(stream)
return _
def test_add_member():
x = Foo()
x.add_member(make_input(["Manhattan", "0"])
# assert that x.notebook has 0 participants and roles
x = Foo()
x.add_member(make_input(["Omega", "ten", "2", "bob", "documentation", "alice", "code"]))
# assert that self.notebook has bob and alice in the expected roles
不过,更好的是,要求输入的代码可能应该与 this 方法完全分开,它应该只需要一个项目名称和一组参与者及其角色(而不是两个单独的列表)。该集合可以是元组列表或字典,但它应该是不允许每个参与者的名称与其角色不匹配的东西。
def add_member(self, name, participants):
self.notebook.add(name, [x[0] for x in participants], [x[1] for x in participants])
def test_add_member():
x = Foo()
x.add_member("Manhattan", [("bob", "documentation"), ("alice", "code")])
# same assertion as before
添加回答
举报