简体   繁体   English

如何将CSV文件作为红宝石黄瓜数据存储库

[英]How to make your CSV file as your ruby cucumber data repository

im just new with ruby cucumber and i want to have a data repository like a csv file, where i can add all my data so it will be easy to maintain and its not hardcoded in my feature files 我是红宝石黄瓜的新手,我想有一个像csv文件这样的数据存储库,在这里我可以添加所有数据,这样将易于维护并且不会在我的功能文件中进行硬编码

can anyone point me to the right direction how to make this possible 谁能指出我正确的方向如何做到这一点

most of the scenario i see are like this 我看到的大多数情况都是这样

  Scenario: go to google
    When I visit at "google.com"
    Then I click the link "image"

I want to make it like this 我要这样

Scenario: go to any website
    When I visit at "website_a"
    Then I click the link "image"

and in my CSV file it can look for the id "website_a" and select the data "google.com" 在我的CSV文件中,它可以查找ID“ website_a”,然后选择数据“ google.com”

Don't use CSV for this. 请勿为此使用CSV。 CSV is OK when transferring data but it's lacking when being used for configuration data. 传输数据时CSV可以,但是用于配置数据时则缺少CSV。

Instead use YAML . 而是使用YAML It's easily read, and great for configuration files. 它易于阅读,非常适合配置文件。 Ruby comes with a YAML parser as part of the Standard Library. Ruby随附YAML解析器,作为标准库的一部分。

Here's how to get started: 入门方法如下:

require 'yaml'

data = {
  'string' => 'foo',
  'array_of_ints' => [1,2,3],
  'int' => 0,
  'boolean' => true
}

puts data.to_yaml
# >> ---
# >> string: foo
# >> array_of_ints:
# >> - 1
# >> - 2
# >> - 3
# >> int: 0
# >> boolean: true

You can see it's easy to read, and, because it's text, it's easy to modify the YAML file using an editor. 您会看到它很容易阅读,并且因为它是文本,所以很容易使用编辑器修改YAML文件。 Reloading the data is just as easy. 重新加载数据同样容易。 If I modify the above code to write to a file I can reload the data and receive a Ruby object containing the data: 如果修改上述代码以写入文件,则可以重新加载数据并接收包含数据的Ruby对象:

require 'yaml'

data = {
  'string' => 'foo',
  'array_of_ints' => [1,2,3],
  'int' => 0,
  'boolean' => true
}

File.write('data.yaml', data.to_yaml)

loaded_data = YAML.load_file('data.yaml')
loaded_data # => {"string"=>"foo", "array_of_ints"=>[1, 2, 3], "int"=>0, "boolean"=>true}

loaded_data['string'] # => "foo"
loaded_data['array_of_ints'] # => [1, 2, 3]
loaded_data['int'] # => 0
loaded_data['boolean'] # => true

You can even imitate a CSV file, though I'm not sure why you'd want to since you have to access records sequentially or by index instead of by being able to directly access them from a hash: 您甚至可以模仿一个CSV文件,尽管我不确定为什么要这么做,因为您必须顺序地或按索引访问记录,而不是能够直接从哈希访问它们:

loaded_data = YAML.load(<<EOT)
---
- [Name,         HR, Avg. ]
- [Mark McGwire, 65, 0.278]
- [Sammy Sosa,   63, 0.288]
EOT
loaded_data # => [["Name", "HR", "Avg."], ["Mark McGwire", 65, 0.278], ["Sammy Sosa", 63, 0.288]]

There are actually a number of different ways you could handle something like this. 实际上,您可以通过多种不同的方式来处理此类问题。 The way I do this for urls is 'Step Argument Transforms' 我对网址执行此操作的方式是“逐步参数转换”

https://github.com/cucumber/cucumber/wiki/Step-Argument-Transforms https://github.com/cucumber/cucumber/wiki/Step-Argument-Transforms

url_transform.rb (put this in your 'support' folder) url_transform.rb(将其放在“支持”文件夹中)

Transform /^(~internet~)$/ do |trans| "http://the-internet.herokuapp.com" end

then in my feature I just use: 然后在我的功能中,我只使用:

Given I go to the "~internet~" page

then in my steps I use: 然后在我的步骤中使用:

Given /^I go to the "(.*)" page$/ do |url|
@browser.goto url
puts browser.url

Terminal output: 终端输出:

http://the-internet.herokuapp.com/

(you don't need the "~" characters I just use them to remind me that they are 'Transforms') (您不需要“〜”字符,我只是用它们来提醒我它们是“变换”)

This is by no means the best solution but if you're a beginner this is probably a useful trick for urls at least. 这绝不是最好的解决方案,但是,如果您是初学者,那么至少对于网址而言,这可能是一个有用的技巧。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ruby-在代码中使用删除文件和目录命令时如何防止擦拭硬盘 - Ruby - How to prevent wiping your hard drive when using delete file and directory commands in your code 如何修复“您的 Ruby 版本为 2.3.3,但您的 Gemfile 指定为 2.5.2” - How to fix 'Your Ruby version is 2.3.3, but your Gemfile specified 2.5.2' 如何修复“您的 Ruby 版本是 1.9.3,但您的 Gemfile 指定了 2.0.0” - How to fix "Your Ruby version is 1.9.3, but your Gemfile specified 2.0.0" 如何制作自己的属性,例如 红宝石/ Rails中的“ devise:authenticable” - How to make your own properties, eg. “devise :authenticable” in ruby / rails 使您的Ruby代码模糊不清? - Obscure your Ruby code? 如何修复“您的 Ruby 版本为 2.3.3,但您的 Gemfile 指定为 2.5.7p206” - How to fix “Your Ruby version is 2.3.3, but your Gemfile specified 2.5.7p206” 如何解决“您的 Ruby 版本是 2.5.3,但您的 Gemfile 指定 2.5.1”部署 AWS eb? - How to fix "Your Ruby version is 2.5.3, but your Gemfile specified 2.5.1" deploying the AWS eb? 如何解决'你的Ruby版本是2.6.3,但是你的Gemfile指定的是2.7.4'怎么办? - How to resolve 'Your Ruby version is 2.6.3, but your Gemfile specified 2.7.4" What should I do? 什么是ruby测试工具,它“破坏”了代码以查看测试的紧密程度? - What is the ruby test tool called that 'breaks' your code to see how tight your tests are? 服务器启动时如何修复“您的 Ruby 版本是 2.3.0,但您的 Gemfile 指定为 2.2.5” - How to fix "Your Ruby version is 2.3.0, but your Gemfile specified 2.2.5" while server starting
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM