使用黄瓜/水豚在哈希中存储和提取数据



我在黄瓜/水豚/site_prism上有很多使用不同登录凭据的测试,这些测试非常混乱。我想尽可能地统一它们;这个解决方案似乎很好 https://blog.jayway.com/2012/04/03/cucumber-data-driven-testing-tips/

但是在遵循示例时,我在步骤定义的第一行遇到了这个问题

Your block takes 1 argument, but the Regexp matched 2 arguments.

显然,我误解了应该如何处理哈希值;有人可以帮忙吗?我的测试数据较少的代码如下黄瓜

Given I login as "ad" with the following data:
    |role|usern       |userpass     |
    |ad  |adcccount   |adpassword   |
    |ml  |mlaccount   |mlpassword   |

步骤定义

Given /^I login as "(ad|ml)" with the following data:/ do |user|
 temp_hash = {}
    if (user == "ad")
      temp_hash = $ad
    elsif (user == "ml")
      temp_hash = $ml
    end
    usern = temp_hash["usern"]
    userpass = temp_hash["userpass"]
 @app = App.new
  @app.login.load
  @app.login.username.set usern
  @app.login.password.set userpass
  @app.login.btn_login.click
end

您会收到该错误,因为匹配的第二个参数是data_table。 您的步骤定义需要

Given /^I login as "(ad|ml)" with the following data:/ do |user, data_table|
  ...

如果您查看链接文章中的Given /I create new user named "(user_1|user_2|user_3)" with the following data:/ do |user, data_table|步骤,您可以看到相同的内容,尽管这不会在data_table中使用多个条目,所以我不能 100% 确定您要在示例中做什么。

谢谢,托马斯; 您的提示导致以下代码按预期工作

Given /^I login as "(ad|ml)" with the following data:/ do |login_role, data|
  temp_hash = {}
  data.hashes.each do |hash|
    if hash[:role] == login_role
      temp_hash[:role] = hash[:role]
      temp_hash[:usern] = hash[:usern]
      temp_hash[:userpass] = hash[:userpass]
    end
  end
    usern = temp_hash[:usern]
    userpass = temp_hash[:userpass]
  @app = App.new
  @app.login.load
  @app.login.username.set usern
  @app.login.password.set userpass
  @app.login.btn_login.click
  expect(@app.dashboard).to be_displayed
end

最新更新