I'm trying to create a unit test for azure function and getting Unsupported expression: Non-overridable members may not be used in setup / verification expressions.
Here is my Function
public class EmployeeFunction
{
private readonly EmpRepo _repo;
public EmployeeFunction(EmpRepo repo)
{
_repo = repo;
}
[FunctionName("EmployeeFunctionTrigger")]
public async Task<IActionResult> Run([HttpTrigger(AuthorizationLevel.Function, "get", Route = "EmployeeFunction/{empName}")] HttpRequest req, string empName, ILogger log)
{
//code
return _repo.GetEmployeeByName(empName);
}
}
And here is my Repository where I'm injecting the IHttpClientFactory
public class EmpRepo : IEmpRepo
{
private readonly IHttpClientFactory _httpClientFactory;
public EmpRepo (IHttpClientFactory clientFactory)
{
_httpClientFactory = clientFactory;
}
public async Task<Employee> GetEmployeeByName(string name)
{
using HttpClient _httpClient = httpClientFactory.CreateClient("EmpClient"){
//code
}
}
And here is my Test. I'm using Xunit and Moq
public class EmpFunctionTest
{
[Fact]
public async Task Http_trigger_should_return_known_string()
{
// Arrange
var httpClientFactory = new Mock<IHttpClientFactory>();
var mockHttpMessageHandler = new Mock<HttpMessageHandler>();
var fixture = new Fixture();
mockHttpMessageHandler.Protected()
.Setup<Task<HttpResponseMessage>>("SendAsync", ItExpr.IsAny<HttpRequestMessage>(), ItExpr.IsAny<CancellationToken>())
.ReturnsAsync(new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent(fixture.Create<String>()),
});
var client = new HttpClient(mockHttpMessageHandler.Object);
client.BaseAddress = fixture.Create<Uri>();
httpClientFactory.Setup(_ => _.CreateClient(It.IsAny<string>())).Returns(client);
var empRepo= new Mock<EmpRepo>(httpClientFactory.Object);
empRepo.Setup(o => o.GetEmployeeByName(It.IsAny<string>()))
// prepare the expected response of the mocked http call
.ReturnsAsync(new Employee()
{
Name= "Test",
ID= 12345,
})
.Verifiable();
var EmpFuc= new EmployeeFunction(empRepo.Object);
//code for act and assert
}
But getting the exception
System.NotSupportedException : Unsupported expression: o => o.GetEmployeeByName(It.IsAny<string>()) Non-overridable members (here: EmpRepo.GetEmployeeByName) may not be used in setup / verification expressions.