ruby - 使用 Aruba/Cucumber 写入标准输入

标签 ruby cucumber bdd functional-testing aruba

我在使用 Aruba 写入标准输入时遇到问题。我尝试了三种方法。

方法一:

Scenario: Write to stdin take 1
  Given a file named "infile" with:
    """
    Hello World!
    """
  When I run `cat < infile`
  Then the output should contain exactly:
    """
    Hello World!
    """

为此我收到以下错误:

  expected: "Hello World!"
       got: "Hello World!cat: <: No such file or directory\n" (using ==)
  Diff:
  @@ -1,2 +1,2 @@
  -Hello World!
  +Hello World!cat: <: No such file or directory
   (RSpec::Expectations::ExpectationNotMetError)
  features/cgi.feature:17:in `Then the output should contain exactly:'

Aruba 是按字面意思传递“<”,而 shell 会使用管道执行一些魔法。

方法 2:

Scenario: Write to stdin take 2
  When I run `cat` interactively
  And I type "Hello World!"
  Then the output should contain:
    """
    Hello World!
    """

我收到以下错误:

  process still alive after 3 seconds (ChildProcess::TimeoutError)
  features/cgi.feature:25:in `Then the output should contain:'

我不知道,但我假设 cat 没有接收到 EOF 字符,因此 cat 保持打开状态,在写入之前等待进一步的输入。有什么方法可以发出输入结束信号吗?

方法 3:

Scenario: Write to stdin take 1
  Given a file named "infile" with:
    """
    Hello World!
    """
  When I run `sh -c "cat < infile"`
  Then the output should contain exactly:
    """
    Hello World!
    """

这种方法可行,但通过 shell 进程传递输入似乎不是理想的解决方案。

我本以为这是一个相当标准的要求,但尚未成功使其发挥作用。

有什么建议吗?

谢谢。

最佳答案

编辑:我尝试使用交互模式通过管道输入文件,但是在实际使用中我发现它比使用 sh -c "process < infile" 慢得多,我不太清楚为什么会这样,在 Ruby 中写入 stdin 可能会产生额外的开销 @interactive.stdin.write(input)或者关闭管道可能需要一段时间@interactive.stdin.close() 。我最终使用了 sh -c避开减速。如果需要跨平台支持,那么我预计较慢的运行时间可能是可以接受的。

原帖:

我找到了几种方法来实现这一目标。

对于拍摄 1:

Scenario: Write to stdin take 1
  Given a file named "infile" with:
    """
    Hello World!
    """
 -When I run `cat < infile`
 +When I run `cat` interactively
 +And I pipe in the file "infile"
  Then the output should contain exactly:
    """
    Hello World!
    """

对于场景 1,我删除了管道 (<) 的尝试,而是以交互方式运行该进程。在后端我写了这一步:

When /^I pipe in the file "(.*?)"$/ do |file|
  in_current_dir do
    File.open(file, 'r').each_line do |line|
      _write_interactive(line)
    end
  end
  @interactive.stdin.close()
end

@interactive.stdin.close() 应该作为函数移至 aruba/api.rb,但这个想法是有效的。对 _write_interactive 的调用也应该是对 type() 的调用,但是 type() 总是添加一个新行,这可能不是我们在文件中进行管道传输时想要的。

对于拍摄 2:

Scenario: Write to stdin take 2
  When I run `cat` interactively
  And I type "Hello World!"
 +Then I close the stdin stream
  And the output should contain:
    """
    Hello World!
    """

我添加了关闭标准输入流,以及后台步骤:

Then /^I close the stdin stream$/ do
  @interactive.stdin.close()
end

这行代码应该再次被制作成 aruba/api.rb 中的方法,但代码可以工作。

关于ruby - 使用 Aruba/Cucumber 写入标准输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12170071/

相关文章:

ruby-on-rails - 部署后缺少 gem (Ruby、Ruby on Rails、Capistrano)

python - 在 Ruby 中,为什么 `Array.length` 会给出 NoMethodError?

ruby-on-rails - 访问使用 attr_accessor 创建的变量

refactoring - 做BDD时如何重构和发现依赖关系

ruby-on-rails - 如何为不同的应用程序使用自定义 ruby​​ 和 rails?

java - 在 java 数据表中转换时的 cucumber 特征文件不会在字符串中给出空值

ruby - 使用 ruby​​ + watir-webdriver + cucumber 和 parallel_tests gem 在多个浏览器中运行测试

c# - 如何获取场景名称和参数?规范流

testing - 使用另一个实用程序服务获取 Karate 请求数据

ruby-on-rails - Cucumber BDD for RAILS/ActiveRecord - 通过填充数据库层来加速慢速测试?