ruby - 用于存储正确答案的 cucumber 数据表的步骤定义

标签 ruby cucumber capybara

我正在尝试找到一种方法来测试多项选择题。其结构是一节课有 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/

相关文章:

ruby-on-rails - 如何运行尚未写入文件的 RSpec::ExampleGroup?

java - 如何使用另一个类的字符串设置 cucumber 特征路径?

java - 添加 JIRA 链接到 Karate/ cucumber 报告

ruby-on-rails - 带有 Capybara 的 Rspec 找不到链接或任何元素

javascript - rspec 单击一个跨度

rspec - 使用 Capybara + RSpec 测试页面重定向

ruby - 你能用 ruby​​ 中的消息从特定错误中解救出来吗?

ruby-on-rails - 使用 rake db :create 创建 Rails DB 时出错

ruby - 在 Ruby 中实现生产者消费者模式

java - Cucable插件: How can I avoid running the '[CUCABLE:FEATURE].feature' (java template file)?