如何最小化注入到另一个的多级接口继承方法class
How to moq the method of multilevel interfaces inheritance which is injected to another class
我的第一个界面
public interface IBaseRepository
{
Task<T> FirstOrDefault<T>(Expression<Func<T, bool>> predicate, Expression<Func<T, object>>[]
includeProperties = null) where T : class;
}
我的第二个界面
public interface IGenericRepository : IBaseRepository
{
//Some methodes;
}
我的服务class和上面注入的方法IGenericRepository
public class AService
{
private readonly IGenericRepository _genericRepository;
public AService(IGenericRepository genericRepository)
{
_genericRepository = genericRepository;
}
public async Task<decimal> SaveRequest(Guid genericId)
{
.
.
var requestType = await _genericRepository.FirstOrDefault<RequestType>(x =>
x.RequestTypeUuid == genericId); //in this line getting error message.
.
.
}
}
我的最小起订量设置在测试 class 上面的服务方法
public class AServiceTests
{
private readonly Mock<IGenericRepository> mockGenericRepository;
private readonly MockRepository mockRepository;
public AServiceTests()
{
this.mockRepository = new MockRepository(MockBehavior.Strict);
this.mockGenericRepository = this.mockRepository.Create<IGenericRepository>();
}
private AService CreateAService()
{
return new AService(this.mockGenericRepository.Object);
}
}
我的测试用例方法
[Fact]
public async Task SaveRequest_SendingValidData_ExpectedNotNull()
{
// Arrange
Guid genericId = new Guid("74jdjd83-8383-3hg7-83jj-e69427931176");
var fakeReturnRequestType = GetFakeReturnRequestType();
mockGenericRepository.Setup(x => x.FirstOrDefault<RequestType>(f => f.RequestTypeUuid ==
genericId, null)).ReturnsAsync(fakeReturnRequestType);
var objAService = this.CreateAService();
var result = await objAService.SaveRequest(genericId);
//Assertion
.
.
}
获取模拟问题:
Message: Moq.MockException :
IBaseRepository.FirstOrDefault(x => (x.RequestTypeUuid ==
value(Services.Impls.AService+<>c__DisplayClass10_0).genericId), null)
invocation failed with mock behavior Strict. All invocations on the
mock must have a corresponding setup. . . AbstractInvocation.Proceed()
IGenericRepositoryProxy.FirstOrDefault[T](Expression1 predicate, Expression
1[] includeProperties) . .
--- End of stack trace from previous location where exception was thrown ---
x.FirstOrDefault<RequestType>(It.IsAny<Expression<Func<RequestType, bool>>>(), ...)
对我有用。
我的第一个界面
public interface IBaseRepository
{
Task<T> FirstOrDefault<T>(Expression<Func<T, bool>> predicate, Expression<Func<T, object>>[]
includeProperties = null) where T : class;
}
我的第二个界面
public interface IGenericRepository : IBaseRepository
{
//Some methodes;
}
我的服务class和上面注入的方法IGenericRepository
public class AService
{
private readonly IGenericRepository _genericRepository;
public AService(IGenericRepository genericRepository)
{
_genericRepository = genericRepository;
}
public async Task<decimal> SaveRequest(Guid genericId)
{
.
.
var requestType = await _genericRepository.FirstOrDefault<RequestType>(x =>
x.RequestTypeUuid == genericId); //in this line getting error message.
.
.
}
}
我的最小起订量设置在测试 class 上面的服务方法
public class AServiceTests
{
private readonly Mock<IGenericRepository> mockGenericRepository;
private readonly MockRepository mockRepository;
public AServiceTests()
{
this.mockRepository = new MockRepository(MockBehavior.Strict);
this.mockGenericRepository = this.mockRepository.Create<IGenericRepository>();
}
private AService CreateAService()
{
return new AService(this.mockGenericRepository.Object);
}
}
我的测试用例方法
[Fact]
public async Task SaveRequest_SendingValidData_ExpectedNotNull()
{
// Arrange
Guid genericId = new Guid("74jdjd83-8383-3hg7-83jj-e69427931176");
var fakeReturnRequestType = GetFakeReturnRequestType();
mockGenericRepository.Setup(x => x.FirstOrDefault<RequestType>(f => f.RequestTypeUuid ==
genericId, null)).ReturnsAsync(fakeReturnRequestType);
var objAService = this.CreateAService();
var result = await objAService.SaveRequest(genericId);
//Assertion
.
.
}
获取模拟问题:
Message: Moq.MockException : IBaseRepository.FirstOrDefault(x => (x.RequestTypeUuid == value(Services.Impls.AService+<>c__DisplayClass10_0).genericId), null) invocation failed with mock behavior Strict. All invocations on the mock must have a corresponding setup. . . AbstractInvocation.Proceed() IGenericRepositoryProxy.FirstOrDefault[T](Expression
1 predicate, Expression
1[] includeProperties) . . --- End of stack trace from previous location where exception was thrown ---
x.FirstOrDefault<RequestType>(It.IsAny<Expression<Func<RequestType, bool>>>(), ...)
对我有用。