I have injected IFlurlClient and i want to write test for it but it throw null refrence exception?
shirin-monzavi opened this issue · comments
Shirin Monzavi commented
If you have a specific programming question, please ask on Stack Overflow. It will be answered more quickly and reach a broader audience. Thanks!
https://stackoverflow.com/questions/ask?tags=flurl
here is sut;
public class CustomerService : ICustomerService
{
private readonly IFlurlClient _flurlClient;
private readonly ILoggerProvider<CustomerService> _logger;
private readonly IConfiguration _configuration;
public CustomerService(ILoggerProvider<CustomerService> logger, IConfiguration configuration, IFlurlClient flurlClient)
{
_logger = logger;
_configuration = configuration;
_flurlClient = flurlClient;
}
public async Task<int> PostCustomerData(GetCustomerOutputDto getCustomerOutputDtos)
{
try
{
var baseUrl = _configuration["ExternalSerivce:BaseUrl"];
var segmentUrl = _configuration["ExternalSerivce:CustomerSegmentUrl"];
_flurlClient.BaseUrl = baseUrl;
var response = await
_flurlClient
.Request(segmentUrl)
.PostJsonAsync(getCustomerOutputDtos);
var s=await response.GetJsonAsync<PostResponse>();
return s.Data;
}
catch (FlurlHttpException ex)
{
_logger.Information(getCustomerOutputDtos, $"Error returned from {ex.Call.Request.Url} {ex.Message}", string.Empty);
return 0;
}
}
}
here is test class
public class CustomerServiceTests
{
#region Feilds
private readonly CustomerService sut;
private readonly Mock<ILoggerProvider<CustomerService>> loggerMock;
private readonly Mock<IConfiguration> configurationMock;
private readonly Mock<IFlurlClient> flurlMock;
private readonly Mock<IFlurlRequest> flurlRequestMock;
private readonly Mock<IFlurlResponse> flurlResponseMock;
#endregion
public CustomerServiceTests()
{
loggerMock = new Mock<ILoggerProvider<CustomerService>>();
configurationMock = new Mock<IConfiguration>();
flurlMock = new Mock<IFlurlClient>();
flurlRequestMock = new Mock<IFlurlRequest>();
flurlResponseMock = new Mock<IFlurlResponse>();
var settingsContainer = new Mock<ISettingsContainer>();
configurationMock.Setup(c => c["ExternalSerivce:BaseUrl"]).Returns(CustomerExternaleServiceConstantTest.BASE_URL);
configurationMock.Setup(c => c["ExternalSerivce:CustomerSegmentUrl"]).Returns(CustomerExternaleServiceConstantTest.CUSTOMER_SEGMENT_URL);
flurlMock.Setup(c => c.Request(It.IsAny<string>())).Returns(flurlRequestMock.Object);
flurlMock.Setup(c => c.BaseUrl).Returns(CustomerExternaleServiceConstantTest.BASE_URL);
var httpMessageHandlerMock = new Mock<HttpMessageHandler>();
flurlMock.Setup(c => c.HttpClient).Returns(new HttpClient(httpMessageHandlerMock.Object)
{
BaseAddress = new Uri(CustomerExternaleServiceConstantTest.BASE_URL),
DefaultRequestVersion = new Version(),
DefaultVersionPolicy = new HttpVersionPolicy(),
MaxResponseContentBufferSize = 10,
Timeout = TimeSpan.FromSeconds(10)
});
flurlRequestMock.Setup(c => c.Settings).Returns(new FlurlHttpSettings());
flurlRequestMock.Setup(c => c.Url).Returns(new Flurl.Url(CustomerExternaleServiceConstantTest.BASE_URL));
flurlRequestMock.Setup(c => c.Client).Returns(flurlMock.Object);
flurlRequestMock.Setup(c => c.Verb).Returns(HttpMethod.Post);
flurlRequestMock.Setup(c => c.CookieJar).Returns(new CookieJar());
flurlResponseMock.Setup(c => c.ResponseMessage).Returns(new HttpResponseMessage()
{
StatusCode = HttpStatusCode.OK,
Content = new StringContent("[{'id':1,'value':'1'}]"),
ReasonPhrase = "",
RequestMessage = new HttpRequestMessage(),
Version = new Version()
});
var HttpContentMock = new Mock<HttpContent>();
flurlResponseMock.Setup(c => c.StatusCode).Returns(200);
sut = new CustomerService(loggerMock.Object, configurationMock.Object, flurlMock.Object);
}
[Fact]
public async Task PostCustomerData_Should_Post_Customer_Data()
{
//Arrenge
var customersData = new GetCustomerOutputDto()
{
GetCusTellDto = new List<GetCusTellDto>(),
GetCustomerDto = new List<GetCustomerDto>(),
GetSarfaslDto = new List<GetSarfaslDto>()
};
//Act
var actual = await sut.PostCustomerData(customersData);
//Assert
actual.Should().BePositive();
}
public class HttpMessageHandlerMock : HttpMessageHandler
{
protected override Task<HttpResponseMessage> SendAsync(
HttpRequestMessage request,
CancellationToken cancellationToken)
{
return Task.FromResult(new HttpResponseMessage());
}
}
}
PostJsonAsync method return null.
anyone can help?
Todd Menier commented
Please post programming questions on Stack Overflow. And it's going to be very difficult for anyone to help you unless you reduce this to a minimal reproducible example.