I am attempting to Mock
an Interface
that has a single Task<string>
method.
The same Q here, though i can't seem to get the code to work in my favor: Setup async Task callback in Moq Framework
My interface looks like this:
public interface IHttpClient
{
Task<string> GetStringAsync(string uri);
}
I am trying to mock this up as so:
var mockHttp = new Mock<IHttpClient>();
mockHttp.Setup(m => m.GetStringAsync("aPretendUrl")).ReturnsAsync("Some sort of string");
I am finding that the result of the GetStringAsync
is null
In the controller, where this instance is injected, I am calling it as so:
string responseData = await _client.GetStringAsync(url);
Also trying
string responseData = _client.GetStringAsync(url).Result;
responseData
is null
in all cases.
I am sure I am missing something simple. Still new with Unit tests
Can anybody point out where I am going wrong?
Update
The full unit test looks like this:
[Test]
public void Given_EController_Called_With_unknown_pedi_Returns_NotFound()
{
// Arrange
AppSettings settings = new AppSettings()
{
DataWarehouseAPI = "http://myurl.com"
};
Mock<IOptionsSnapshot<AppSettings>> mockSettings = new Mock<IOptionsSnapshot<AppSettings>>();
mockSettings.Setup(m => m.Value).Returns(settings);
var mockHttp = new Mock<IHttpClient>();
mockHttp.Setup(m => m.GetStringAsync("aPretendUrl")).ReturnsAsync("[]");
EntryController controller = new EntryController(mockHttp.Object, mockSettings.Object);
// Act
IActionResult actionResult = controller.GetByPedimento("nothing").Result;
// Assert
Assert.IsAssignableFrom<NotFoundObjectResult>(actionResult);
}