'How to moq the method of multilevel interfaces inheritance which is injected to another class
My First Interface
public interface IBaseRepository
{
Task<T> FirstOrDefault<T>(Expression<Func<T, bool>> predicate, Expression<Func<T, object>>[]
includeProperties = null) where T : class;
}
My Second Interface
public interface IGenericRepository : IBaseRepository
{
//Some methodes;
}
My service class and method to which injected above 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.
.
.
}
}
My moq setup in test class for above service methode
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);
}
}
My test case method
[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
.
.
}
getting the mocking issue :
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, Expression1[] includeProperties) . . --- End of stack trace from previous location where exception was thrown ---
Solution 1:[1]
Use the methods getText() and getLinkUrl()
Sample based on your first code snippet:
function myFunction() {
var doc = DocumentApp.getActiveDocument();
var linku = doc.getBody().getTables()[0].getCell(2,1);
Logger.log("link text is: " + linku.getText());
Logger.log("link is: " + linku.getLinkUrl());
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ziganotschka |
