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

関数にする?
関数の説明をdescribeブロックに入れる

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 に答える