【问题标题】:Best way to mock out StringIO input for Ruby unit test?为 Ruby 单元测试模拟 StringIO 输入的最佳方法?
【发布时间】:2014-01-01 18:11:36
【问题描述】:

有没有更好的方法来模拟 StringIO 输入?我正在专门寻找一种避免调用 .rewind 方法的方法。我尝试使用 StringIO.open 的块形式,但它在返回时将内部字符串归零。

test_input = StringIO.new
test_input.puts '### Functional Tags'
test_input.puts '"@api"   *Workflow API*'
test_input.puts '"@categorize"'
test_input.puts ''
test_input.puts '"@combine"'
test_input.puts '"@flaky"  *delicate tests*'
test_input.rewind

【问题讨论】:

    标签: ruby unit-testing ruby-1.9.3 stringio


    【解决方案1】:

    怎么样:

    test_input = StringIO.new <<-RUBY
    ### Functional Tags'
    "@api"   *Workflow API*
    "@categorize"
    
    "@combine"
    "@flaky"  *delicate tests*
    RUBY
    

    【讨论】:

    • 完美,但我还有一个问题。这是否可以通过使用 heredoc 语法创建一个字符串文字然后将其传递给 StringIO 的新方法来实现?
    猜你喜欢
    • 2011-02-02
    • 2014-04-16
    • 2015-10-21
    • 2017-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-14
    相关资源
    最近更新 更多