我有一個ruby script,它在運行時從用戶處獲取書籍的價格。我很少爲Ruby腳本編寫RSpec測試。我使用的是Ruby 1.9.3-p327和rspec 2.11.0。你可以克隆我的項目從Githubthis link。針對Ruby腳本的Rspec測試問題,需要用戶輸入散列元素
我的Rspec測試旨在測試新實例化的對象是否屬於特定的類。
如果我在ruby script中註釋行號32,我的測試成功通過。
不知何故,當我取消註釋該行,我得到下面的錯誤是用戶輸入。我甚至沒有將其作爲我的spec file的一部分進行測試,但仍然遇到了這個錯誤。我不清楚爲什麼以及如何解決這個問題。
The Last Samurai: /home/mohnish/xxx/yyy/sample_pocs/book/book.rb:11:in `gets': Is a directory - spec (Errno::EISDIR)
from /home/mohnish/xxx/yyy/sample_pocs/book/book.rb:11:in `gets'
from /home/mohnish/xxx/yyy/sample_pocs/book/book.rb:11:in `block in get_prices'
from /home/mohnish/xxx/yyy/sample_pocs/book/book.rb:9:in `each'
from /home/mohnish/xxx/yyy/sample_pocs/book/book.rb:9:in `inject'
from /home/mohnish/xxx/yyy/sample_pocs/book/book.rb:9:in `get_prices'
from /home/mohnish/xxx/yyy/sample_pocs/book/book.rb:49:in `<top (required)>'
from /home/mohnish/xxx/yyy/sample_pocs/book/spec/spec_helper.rb:1:in `require_relative'
from /home/mohnish/xxx/yyy/sample_pocs/book/spec/spec_helper.rb:1:in `<top (required)>'
from /home/mohnish/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /home/mohnish/.rvm/rubies/ruby-1.9.3-p327/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /home/mohnish/xxx/yyy/sample_pocs/book/spec/book_spec.rb:1:in `<top (required)>'
from /home/mohnish/.rvm/gems/[email protected]_set/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `load'
from /home/mohnish/.rvm/gems/[email protected]_set/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `block in load_spec_files'
from /home/mohnish/.rvm/gems/[email protected]_set/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `map'
from /home/mohnish/.rvm/gems/[email protected]_set/gems/rspec-core-2.11.1/lib/rspec/core/configuration.rb:780:in `load_spec_files'
from /home/mohnish/.rvm/gems/[email protected]_set/gems/rspec-core-2.11.1/lib/rspec/core/command_line.rb:22:in `run'
from /home/mohnish/.rvm/gems/[email protected]_set/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:69:in `run'
from /home/mohnish/.rvm/gems/[email protected]_set/gems/rspec-core-2.11.1/lib/rspec/core/runner.rb:8:in `block in autorun'
我也想測試這個例子的用戶輸入。如果你能夠點亮我怎麼能得到同樣的結果,這將是非常好的。我發現有些地方可以開始測試用戶輸入,如eg 1和eg 2,但我主要是看如何測試用戶輸入的一些屬於散列的元素
謝謝。
謝謝肖恩。這解決了錯誤。我可能應該將我的搜索精煉到「'gets」:是一個目錄 - spec「。我學習的方式.. :)。關於如何編寫用戶輸入哈希元素的規範的任何輸入? – boddhisattva
用戶正在輸入一些被轉換爲散列的字符串,或者該方法期待實際的散列? –
用戶輸入是一個字符串,每個這樣的字符串都存儲爲哈希中每個關鍵元素的值。 – boddhisattva