我试图更好地了解如何使用class Answer extends Component {
constructor(props){
super(props);
state = {
value: this.props.answerText,
isTrue: false,
isChecked: false,
question_01:"",
question_02:"",
}
this.check=this.check.bind(this)
}
// shouldComponentUpdate(nextProps: Readonly<AnswersProps>, nextState: Readonly<AnswersState>, nextContext: any): boolean {
// return this.state.value !== nextProps.value;
// }
// componentDidUpdate(prevProps: Readonly<AnswersProps>, prevState: Readonly<AnswersState>, snapshot?: any): void {
// console.log(this.state.isChecked)
// }
check(e,question){
var data=this.state;
data[question]=e.target.value
this.setState({data})
}
render () {
return
<label>
{/* First question */}
<input type="radio" name={this.props.name1} checked={this.state.question_1==="value11"} onClick={(e)=>this.check(e,'question_1') } value="value11"/>{ this.props.answerText }
<input type="radio" name={this.props.name1} checked={this.state.question_1==="value12"} onClick={(e)=>this.check(e,'question_1') } value="value12"/>{ this.props.answerText }
{/* Second question */}
<input type="radio" name={this.props.name2} checked={this.state.question_2==="value21"} onClick={(e)=>this.check(e,'question_2') } value="value21"/>{ this.props.answerText }
<input type="radio" name={this.props.name2} checked={this.state.question_2==="value22"} onClick={(e)=>this.check(e,'question_2') } value="value22"/>{ this.props.answerText }
</label>
}
}
进行单元测试,并且遇到无法确定最佳方法的情况。我在SO上发现了一些类似的问题,这些问题涉及如何对夹具本身进行参数化,但是我不认为这是我想要做的。但是也许我还没有完全了解灯具本身?
以下是一些示例代码:
pytest
class Person:
def __init__(self, fname: str, lname: str):
self.fname = fname
self.lname = lname
def upper_fullname(self):
return f'{self.fname} {self.lname}'.upper()
我希望这两个测试都能通过,但是我收到一条错误消息,提示import pytest
@pytest.fixture(scope="module")
def fixture_mm(conf={"fname": "mickey", "lname": "mouse"}):
return Person(**conf)
@pytest.fixture(scope="module")
def fixture_bb(conf={"fname": "bugs", "lname": "bunny"}):
return Person(**conf)
@pytest.mark.parametrize(
'person, expected_result',
[(fixture_mm, "MICKEY MOUSE"), (fixture_bb, "BUGS BUNNY")])
def test_uppernames(person, expected_result):
assert person.upper_fullname() == expected_result
。我在这里想念什么?
谢谢。
答案 0 :(得分:1)
由于fixture_mm
是一个函数,因此出现错误。您需要调用一个函数以获取值(fixture_mm()
),但这不适用于夹具。
但我认为您甚至不需要在这里安装灯具。您可以将它们设置为正常功能并像这样使用它们:
def person_mm(conf={"fname": "mickey", "lname": "mouse"}):
return Person(**conf)
def person_bb(conf={"fname": "bugs", "lname": "bunny"}):
return Person(**conf)
@pytest.mark.parametrize(
'person, expected_result',
[(person_mm(), "MICKEY MOUSE"), (person_bb(), "BUGS BUNNY")])
def test_uppernames(person, expected_result):
assert person.upper_fullname() == expected_result
注意:由于在模块编译时评估了夹具参数,因此如果功能需要一些设置,则可能会导致问题。另一方面,如果您的函数像本示例中一样简单,则甚至可以将这些对象保存为常量:
PERSON_MM = Person(fname="mickey", lname="mouse")
PERSON_BB = Person(fname="bugs", lname="bunny")