I have the following method to test:
public static DateTime? GetLastSourceUpdateDate(this IQueryableDataFacade facade, long idDevice, long idMeasureType)
{
return facade.ExecuteExpression<SynchonizerLastUpdate, DateTime?>(logs =>
{
return logs.Where(log => log.IdDeviceMeasureType == idMeasureType && log.Device.Id == idDevice).Select(r => r.LastUpdateDate).FirstOrDefault();
});
}
So, I need to mock the interface IQueryableDataFacade
for this. I have tried:
[Fact]
public void ItReturnsNullWhenNoValueIsPresentForThatEntity()
{
List<SynchonizerLastUpdate> datasource = new List<SynchonizerLastUpdate>() {
new SynchonizerLastUpdate() { Id = 1, Device = new Device() { Id = 1}, IdDeviceMeasureType = 1, LastUpdateDate = new DateTime(2020, 1, 1) },
new SynchonizerLastUpdate() { Id = 1, Device = new Device() { Id = 1}, IdDeviceMeasureType = 1, LastUpdateDate = new DateTime(2020, 1, 1) },
new SynchonizerLastUpdate() { Id = 1, Device = new Device() { Id = 1}, IdDeviceMeasureType = 1, LastUpdateDate = new DateTime(2020, 1, 1) }};
var facadeService = new Mock<IQueryableDataFacade>();
facadeService.Setup(m => m.ExecuteExpression<SynchonizerLastUpdate, DateTime?>(It.IsAny<Func<IQueryable<SynchonizerLastUpdate>, DateTime?>>())).Returns((p) => { datasource.Select(p)});
}
But it's not compiling, there's an error on the return type of the mocked method. It says a semicolon is missing, but if I add it, it brokes again, and I'm having troubles to fix it.
question from:
https://stackoverflow.com/questions/65944758/need-help-mocking-interface 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…