我正在尝试找到一种方法来测试多项选择题。其结构是一节课有 8 个阶段,每个阶段包含多项选择题,只有一个正确答案。每次加载问题都是随机的,所以我一直在寻找最好的方法来测试是否点击了正确答案。出于这个原因,我创建了一个包含两列的数据表,它显然比这更广泛,但遵循以下原则:
| what is the opposite of true | false |
| what comes after tuesday | wednesday |
在功能测试中我写到它正在测试正确答案匹配。后来我希望找到一种方法来测试如果问题和答案匹配不在数据表中则它是不正确的。有人能解释一下我将如何为此做测试定义吗?
我尝试使用 rows_hash 方法,但出现以下错误
undefined method `rows_hash' for -3634850196505698949:Fixnum (NoMethodError)
Given(/^a list of answer\-value pairs$/) do |table|
@question_answer_table = hash
end
When(/^I choose a match$/) do
hash = @question_answer_table
@question_answer_table.rows_hash
return false if hash[question].nil?
return hash[question] == answer
end
最佳答案
我认为 rows_hash方法会帮助你。
def question_correct?(cucumber_table, question, answer)
hash = cucumber_table.rows_hash
return false if hash[question].nil?
return hash[question] == answer
end
该代码通过将两列数据表转换为散列来工作,其中第一列是键,第二列是值。
请记住,此方法要求您的数据表限制为两列。
关于ruby - 用于存储正确答案的 cucumber 数据表的步骤定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30242491/