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)
,但我寧願使用上面陳述的多布爾檢查形式。