2015-11-04 69 views
1

我試圖讓我的測試儘可能可讀和可維護,我希望有這種形式的參數對象的驗證傳遞給我提供給SUT的模擬。Moq驗證參數與多個布爾條件

_emailHelperMock.Verify(eh => eh.OnMailCreated(
      It.Is<MailMessage>(WithBodyContaining(enrolment) && SentTo(Test.CustomerAddress)))); 

但我並沒有找到一個解決方案如何做到這一點,因爲我得到的編譯錯誤:

Error CS0019 Operator '&&' cannot be applied to operands of type 'Expression<Func<MailMessage, bool>>' and 'Expression<Func<MailMessage, bool>>' 

語法糖的方法我上面用下列方式定義:

private static Expression<Func<MailMessage, bool>> WithBodyContaining(Enrolment enrolment) 
    { 
     return mail => mail.Body.Contains(enrolment.Title) && mail.Body.Contains(enrolment.Date); 
    } 

    private static Expression<Func<MailMessage, bool>> SentTo(string address) 
    { 
     return mail => mail.To.ToString().Contains(address); 
    } 

我可以做一個單一的方法,例如WithBodyContainingEnrolmentAndSentToAddress(enrolment, address),但我寧願使用上面陳述的多布爾檢查形式。

回答

0

如果您更改方法返回bool s而不是Expression<Func<MailMessage, bool>> s那麼您可以將它們合併,並且在我看來它們會更清晰。例如:

_emailHelperMock.Verify(eh => eh.OnMailCreated(It.Is<MailMessage>(mail => WithBodyContaining(mail, enrolment) && SentTo(mail, Test.CustomerAddress) 

private static bool WithBodyContaining(MailMessage mail, Enrolment enrolment) 
{ 
    return mail.Body.Contains(enrolment.Title) && mail.Body.Contains(enrolment.Date); 
} 

private static bool SentTo(MailMessage mail, string address) 
{ 
    return mail.To.ToString().Contains(address); 
}