2012-07-31 51 views
0

兩個例子都是STDOUT,但黃瓜只能看到第一個。第二個方案失敗:爲什麼黃瓜與阿魯巴沒有看到我的程序輸出?

Then the stdout should contain "test" # aruba-0.4.11/lib/aruba/cucumber.rb:82 
    expected "" to include "test" (RSpec::Expectations::ExpectationNotMetError) 
    features/test.feature:13:in `Then the output should contain "test"' 

特點:

Scenario: echo test 
    Given a blank slate 
    When I run `echo "test"` 
    The stdout should contain "test" 

Scenario: puts test 
    Given a blank slate 
    When I start the program 
    The stdout should contain "test" 

步驟定義:

When /^I start the program$/ do 
    TestModule::Main.new.start 
end 

代碼:

module TestModule 
    class Main 
    def initialize 
    end 
    def start 
     $stdout.puts "test" 
    end 
    end 
end 

回答

0

我沒那麼熟悉與阿魯巴,但快速窺視它的source code建議它針對STDOUT(或任何輸出)僅限於的聲明適用於自身啓動的進程,而不適用於已寫入STDOUT的所有內容。在第二種情況下,您自己調用的代碼不在Aruba的控制範圍之內,因此它的輸出將不會被跟蹤。

如果你仔細想想,它不能真正的工作任何其他方式 - 如果阿魯巴捕獲的所有標準輸出的斷言,那麼它將包含黃瓜自己的測試輸出,以及...

它看起來像你」重新嘗試在進程中測試程序而不使用Aruba來調用單獨的Ruby進程。如果是這種情況,我建議修改程序,以便有可能通過STDOUT替換,例如,

def initialize(output=$stdout) 

然後,當你啓動代碼:

When /^I start the program$/ do 
    TestModule::Main.new(@output).start 
end 

,你可以改變你的斷言:

Then the stdout should contain "(.+)" do |string| 
    @output.should include string 
end 
+0

謝謝。這個建議遵循RSpec Book的模式。我仍然不明白「它自己開始的過程」和「你自己調用的代碼」之間的區別是什麼。是不是在第一個斷言中,黃瓜大概是分叉進程來運行echo,但是一個新的類實例在同一個黃瓜進程中運行?無論如何,我認爲在* nix中,只有一個STDOUT。黃瓜怎麼知道它們的區別? – jordanpg 2012-08-01 16:33:14

+0

看起來好像Aruba正在使用[childprocess](https://github.com/jarib/childprocess)gem來處理運行流程,這可能有助於瞭解發生了什麼 - 頭版上的文檔當然暗示消費者可以選擇如何處理新流程的標準輸出。我認爲'只有一個STDOUT'不必適用於分叉進程,所以Aruba可以自由地只查看對它感興趣的輸出。 – 2012-08-01 18:25:11

+0

現在我明白了。 Aruba使用'ChildProcess'在echo命令的情況下創建一個新進程,並使用'ChildProcess'方法來檢查新進程的標準輸出。在後一種情況下,它正在查看不存在的子進程的標準輸出。 – jordanpg 2012-08-01 18:46:32

相關問題