我有一种情况,我必须测试返回T的任务的方法,我不知道如何设置模拟 这是一个例子:
public interface IRepository<T> : IDisposable where T:EntityBase
{
Task<T> FindAsync(params object[] keyValues);
}
模拟
的配置 public class GenericConfiguration<T> where T : EntityBase
{
public Mock<IPersistenceService> MockPersistence { get; set; }
public Mock<ILogService> MockLog { get; set; }
public Mock<IReadOnlyRepository<T>> MockReadOnlyRepository { get; set; }
public Mock<IRepository<T>> MockEntity { get; set; }
public MockRepository MockRepository { get; set; }
public void Setup()
{
MockRepository = new MockRepository(MockBehavior.Loose) { DefaultValue = DefaultValue.Mock };
MockPersistence = MockRepository.Create<IPersistenceService>();
MockLog = MockRepository.Create<ILogService>();
MockEntity = MockRepository.Create<IRepository<T>>();
MockReadOnlyRepository = MockRepository.Create<IReadOnlyRepository<T>>();
}
/// <summary>
/// Setup mock for FindAsync method
/// </summary>
/// <param name="results"> </param>
public void SetupMockEntityRepositoryForFindAsync(List<T> results)
{
SetupMocForPersistence();
MockEntity.Setup(r => r.FindAsync(It.IsAny<object>())).Returns(Task.FromResult<T>(null));
}
}