2014-03-07 42 views
0

我很喜歡RSpec,但爲了讓我的測試更加徹底和乾爽,我努力尋找一種有效的方法來測試一個完整的例子表。到目前爲止,我已經通過編寫參數化函數來管理這些函數,我可以從每個示例中調用這些函數,但它感覺很詭異,而且還是相當重複的。RSpec的黃瓜般的輪廓?

像黃瓜的情景東西概括和this old gem對RSpec的是什麼我在尋找:

describe "Adding or multiplying two numbers" do 
    outline do 
    it "should return the sum" 
     (a + b).should == sum 
    end 

    it "should return the product" 
     (a * b).should == product 
    end 
    end 

    fields :a, :b, :sum, :product 
    values 1, 1, 2,  1 
    values -1, -2, -3,  2 
    values -1, 1, 0,  -1 
end 

不幸的是,我掛的寶石是3歲。還有其他的東西作爲RSpec的一部分還是另一個完成這個目標的寶石?

+0

我最後一次檢查是在'rspec'不可用和'RSpec的-core'團隊表示,他們正在研究它,但這就像3-4個月前 – bjhaid

回答

1

您可以創建錶行的數組,然後通過實例迭代行:

describe "Adding or multiplying two numbers" do 
    values = Array.new 
    values << {:a => 1, :b => 1, :sum => 2, :product => 1} 
    values << {:a => -1, :b => -2, :sum => -3, :product => 2} 
    values << {:a => -1, :b => 1, :sum => 0, :product => -1} 

    values.each do |value| 
     a = value[:a] 
     b = value[:b] 
     sum = value[:sum] 
     product = value[:product] 

     it "should return the sum" do 
      (a + b).should == sum 
     end  

     it "should return the product" do 
      (a * b).should == product 
     end 
    end 
end