2013-04-09 54 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