2013-04-09 55 views
0

這是我的規格:如何合併rspec中的匹配器?

it "should convert doc successfully" do 
     @response = SharpOffice::Office.process(File.expand_path("spec/fixture/test.doc")) 
     @response[:status].should == 'ok' 
     File.exist?(@response[:pdf_path]).should be_true 
     File.exist?(@response[:swf_path]).should be_true 
     File.exist?(@response[:cover_path]).should be_true 
    end 

    it "should convert ppt successfully" do 
     @response = SharpOffice::Office.process(File.expand_path("spec/fixture/test.ppt")) 
     @response[:status].should == 'ok' 
     File.exist?(@response[:pdf_path]).should be_true 
     File.exist?(@response[:swf_path]).should be_true 
     File.exist?(@response[:cover_path]).should be_true 
    end 

    it "should convert xls successfully" do 
     @response = SharpOffice::Office.process(File.expand_path("spec/fixture/test.xls")) 
     @response[:status].should == 'ok' 
     File.exist?(@response[:pdf_path]).should be_true 
     File.exist?(@response[:swf_path]).should be_true 
     File.exist?(@response[:cover_path]).should be_true 
    end 

如何合併重複?謝謝

回答

1

你可以在一個新的conversion_helpers.rb文件中聲明的自定義匹配:

RSpec::Matchers.define :be_converted_successfully do 
    match do |conversion_response| 
    conversion_response[:status] == 'ok' && File.exist?(conversion_response[:pdf_path]) && File.exist?(conversion_response[:swf_path]) && File.exist?(conversion_response[:cover_path]) 
    end 
end 

然後在你的天賦,require 'conversion_helpers',你可以這樣做:

it "should convert doc successfully" do 
    SharpOffice::Office.process(File.expand_path("spec/fixture/test.doc")).should be_converted_successfully 
end 

it "should convert ppt successfully" do 
    SharpOffice::Office.process(File.expand_path("spec/fixture/test.ppt")).should be_converted_successfully 
end 

it "should convert xls successfully" do 
    SharpOffice::Office.process(File.expand_path("spec/fixture/test.xls")).should be_converted_successfully 
end 

雖然在實際測試中,這可能會非常煩人,試圖追查一個錯誤。但這是一個不同的問題。

0

使它成爲一個函數?
把功能描述中描述塊

def convert_expectation(resp) 
    resp[:status].should == 'ok' 
    File.exist?(resp[:pdf_path]).should be_true 
    File.exist?(resp[:swf_path]).should be_true 
    File.exist?(resp[:cover_path]).should be_true 
end 

it "should bla blabla" do 
    resp = SharpOffice::Office.process(File.expand_path("spec/fixture/test.xls")) 
    convert_expectation(resp) 
end