I'm trying to test a method using xUnit and Moq.
The public void DoSomeWork_Verify_GetJsonDataString_is_called()
test is failing. Please can someone let me know what I am missing in the test/mocking. The GetJsonDataString
is an API call that returns a JSON string.
public class A
{
private readonly IData _data;
private readonly IApiCall _apiCall;
public A (IData data, IApiCall apiCall)
{
_data = data;
_apiCall = apiCall;
}
public void DoSomeWork
{
var apps = _data.GetListOfApps(); // test on this method call is passing
foreach(var app in apps)
{
if(string.IsNullOrEmpty(app.AppId)) continue;
string jsonString = _apiCall.GetJsonDataString("ApiEndpoint" + app.AppId);
// how do I test this method call? Note: I'm not using async, this is just a console app.
}
}
}
//MyTest
public class TestA
{
private readonly Mock<IData> _data;
private readonly Mock<IApiCall> _apicall;
public TestA()
{
_data = new Mock<IData>;
_apiCall = new Mock<IApiCall>;
}
public void DoSomeWork_Verify_GetListOfApps_is_called()
{
_data.Setup(x => x.GetListOfApps());
var sut = new A(_data.Object, _apiCall.Object);
sut.DoSomeWork();
_data.Verify(x => x.GetListOfApps(), Times.AtLeastOnce); // this passes
}
public void DoSomeWork_Verify_GetJsonDataString_is_called()
{
_apicall.Setup(x => x.GetJsonDataString(It.IsAny<string>()));
var sut = new A(_data.Object, _apiCall.Object);
sut.DoSomeWork();
_apicall.Verify(x => x.GetJsonDataString(It.IsAny<string>()), Times.AtLeastOnce); // Failing
}
}