0

这是我的规格:

   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

如何合并重复?谢谢

4

2 回答 2

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

虽然,在实际测试中,试图追踪错误可能会很烦人。但这是一个不同的问题。

于 2013-04-09T08:19:12.853 回答
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
于 2013-04-09T08:17:55.410 回答