Verifying a method called with certain derived parameter
Consider the following snippet;
public enum ReportType {Monthly,Quarterly}
public class BaseReport
{
public ReportType ReportType {get;set;}
}
public class MonthlyReport : BaseReport
{
public String month = "January"
public MonthlyReport() { ReportType = Monthly;}
}
public class Foo
{
public virtual void AddReport(BaseReport report);
}
[Test]
public void Test1()
{
var mock = new Mock<Foo>(){CallBase =true};
var report = new MonthlyReport();
mock.Object.AddReport(report);
}
Well I am trying to verify if the AddReport() is called with certain parameter here;
mock.Verify(x => x.AddReport(It.Is<MonthlyReport>(p => p.month == "January" &&a开发者_Python百科mp;
p.ReportType == ReportType.Monthly)));
As I feared, it doesn't work with a MonthlyReport parameter for Is<> even though it is derived from BaseReport. If I use Is,then I can't use p.month in the expression, and I am not that proficient with c# to know whether I can use if(p is MonthlyReport) in a lambda expression or more importantly, it would work as intended.
How can I approach this problem? Please note that the mock is partial, Although I can live with Setup approach with callbacks if it is neatly solves my problem. Any pointers would be greatly appreciated...
精彩评论