ruby-on-rails
  • ruby-on-rails-3
  • rspec
  • rspec2
  • 2011-03-20 28 views 1 likes 
    1

    在rspec的,我有一個規範,看起來是這樣的:RSPEC,乾燥的測試與使用情況100S

    require 'spec_helper' 
    
    describe IncomingMailsController do 
    
        include Devise::TestHelpers 
    
        it "should find the correct text in the sample" do 
        sample_text = '100s of these' 
        target_text = 'find me' 
        .... ALL Kinds of stuff to process (30+ lines) 
        thread.content.should == 'find me' 
        end 
    
    end 
    

    是否有使用RSpec的方式,讓我莫名其妙地使用所有邏輯一個塊?然後創建它的「xxxxx」100s做,並通過一個變量作爲示例文本?

    感謝

    回答

    4

    你可以把it試塊在循環中,並貫穿所有要分別用於次集合(在下面的例子中這些項目是search_items集合中)

    require 'spec_helper' 
    
    describe IncomingMailsController do 
    
        include Devise::TestHelpers 
    
        search_items.each do |item| 
        it "should find the correct text (#{item}) in the sample" do 
         sample_text = '100s of these' 
         target_text = item 
         .... ALL Kinds of stuff to process (30+ lines) 
         thread.content.should == 'find me' 
        end 
        end 
    
    end 
    

    UPDATE

    我看到在這個threase的另一個答案的評論中,你有一個包含100多個樣本文件的目錄,你可以使用Dir#glob獲得文件名列表。然後,您可以在循環中使用它來生成it測試用例。

    更新2

    Dir.glob('/path/to/files/*.txt').each do |file_name| 
        it "should find the correct text in the sample #{file_name}" do 
         file_content = File.open(file_name, "rb") 
         sample_text = '100s of these' 
         target_text = 'fine me' 
         .... ALL Kinds of stuff to process (30+ lines) 
         thread.content.should == 'find me' 
        end 
        end 
    

    你可能有FILE_NAME的完整路徑玩,但應該給你一些的方式。

    +0

    謝謝ctcherry,集合是一個大的文件目錄。所以我不知道這是否會起作用? – AnApprentice 2011-03-20 19:56:34

    +0

    正在創建一個可能的幫手嗎?我可以製作100個特定於場景的塊,然後在裏面使用助手? – AnApprentice 2011-03-20 19:57:00

    +0

    不錯的更新。你能舉一個例子嗎? thxs – AnApprentice 2011-03-20 19:58:44

    1

    不知道這是你要找的東西,但如何:

    ['xxxxxx', 'yyyyyyy', 'zzzzzzzz'].each do |thing| 
        it "finds #{thing} in the sample do 
        # and so on... 
        end 
    end 
    

    當然,你可能會選擇比用於存儲你的測試字符串數組以外的東西,但這應該讓你開始。

    +0

    謝謝史蒂夫。不知道這是否會奏效。我創建了一個包含100多個文本文件的樣本目錄。每個文本文件在處理時應返回「找我」。爲了達到這一點,它需要很多我想幹的代碼。所以我想知道是否有一種方法可以與RSPEC的助手一起幹呢?或者也許在一個描述中包含所有文件? IDK?剛剛學習所有這些rpsec的東西現在thxs – AnApprentice 2011-03-20 19:54:59

    相關問題