2013-04-16 263 views

回答

3

你可以簡單地循環:

["[email protected]", "Email"].each { |str| last_email_sent.body.should include str } 

或者,如果你喜歡的匹配語法,寫自己的匹配:

RSpec::Matchers.define :include_all do |include_items| 
    match do |given| 
    @errors = include_items.reject { |item| given.include?(item) } 
    @errors.empty? 
    end 

    failure_message_for_should do |given| 
    "did not include \"#{@errors.join('\", \"')}\"" 
    end 

    failure_message_for_should_not do |given| 
    "everything was included" 
    end 

    description do |given| 
    "includes all of #{include_items.join(', ')}" 
    end 
end 

調用它像這樣:

last_email_sent.body.should include_all ["[email protected]", "Email"] 
+0

注的第一份工作沒工作,'| include_items |'輸入需要去對整個匹配定義 –

+0

我想弄清楚如何扭轉這種匹配到''不應該包括any'' – tomekfranek

+1

@regedarek:主要是你需要注意的雙重否定。剛剛實施新的匹配include_any(因爲'should_not include_all'不一樣'應該include_any'),使'failure_message_for_should_not'是比我上面一點更加有用。提示:你不能在正數情況下使用@errors數組,所以'match'模塊應該非常簡單。 。 。 –

1

試試這個

["[email protected]", "Email"].all?{ |str| last_email_sent.body[str] }.should == true 
+0

那麼也許吸塵器將是:''be_in_body.each {|串| last_email_sent.body.should包括字符串}'' – tomekfranek

+0

@regedarek,是的,你_should_是正確的:) – fl00r

0

如果last_email_sent.body正是Company Name SomeCompany [email protected] Email取代它:

last_email_sent.body.should include ["Company Name", "SomeCompany", "[email protected]", "Email"].join(" ") 
0

我喜歡把陣列一樣,在一個實用方法:

規格/支持/ utilities.rb

def email_body_elements 
    ["Company Name", "Some Company", "Email", "[email protected]"] 
end 

規格/ your_spec.rb

email_body_elements.each do |element| 
    last_email_sent.body.should include element 
end 
相關問題