2011-04-02 15 views
8

Bir grup farklı test vakasını RSpec ile test etmenin en iyi yolu nedir? Örneğin RSpec Senaryo Anahatları: Çoklu Test Durumları

, string-additions.rb verilen: Ben rspec string-additions.rb --color --format doc çalıştırdığınızda

require 'rspec' 

class String 
    if method_defined? :reverse_words 
    raise "String#reverse_words is already defined" 
    end 
    def reverse_words 
    split(' ').reverse!.join(' ') 
    end 
end 

describe String do 
    describe "#reverse_words" do 
    specify { "hello".reverse_words.should eq("hello") } 
    specify { "hello world".reverse_words.should eq("world hello") } 
    specify { "bob & pop run".reverse_words.should eq("run pop & bob") } 
    end 
end 

, alıyorum:

String 
    #reverse_words 
    should == hello 
    should == world hello 
    should == run pop & bob 

Ancak, mantıklı çıkış almak istiyorum, böyle:

String 
    #reverse_words 
    "hello" => "hello" 
    "hello world" => "world hello" 
    "bob & pop run" => "run pop & bob" 

Ayrıca, özelliklerim için DRY ürünümün biraz bitmesini istiyorum. RSpec, bu tür çoklu vaka testini DRYing için bir şablon sağlıyor mu? Cucumber scenario outlines ile benzer bir şey?

Not: Bu soru Is there an equivalent in RSpec to Cucumber's “Scenarios” or am I using RSpec the wrong way?'a benzer, ancak Salatalık yerine RSpec ile test edilmesi gereken bir örnek sağlar.

describe String do 
    describe "#reverse_words" do 
    strings = { 
     "hello"   => "hello", 
     "hello world" => "world hello", 
     "bob & pop run" => "run pop & bob" 
    } 

    strings.each do |k, v| 
     specify "\"#{k}\" => \"#{v}\"" do 
     k.reverse_words.should eq(v) 
     end 
    end 
    end 
end 

Bu istediğim çıktıyı verir ama RSpec şeyler bile kurutucu yapmak için bir şablon olsaydı daha güzel olurdu:

cevap