I have a controller class with 2 service interfaces: one is a post method of external URL and the second is a local repository. In the controller test class, I just want to moq the interface1, not the second. How can I do this? Or what is the best way to test this controller?
[Route("api/MyAPI")]
[ApiController]
public class MyAPIController : Controller
{
private readonly Interface1 _interface1;
private readonly Interface2 _interface2;
public ProfitLossGrowthRateController(Interface1 interface1,
Interface2 interface2)
{
_interface1= interface1;
_interface2 = interface2;
}
[HttpPost("CodeAPI")]
[Produces(MediaTypeNames.Application.Json)]
[ProducesResponseType(typeof(List<Response>), StatusCodes.Status200OK)]
[ProducesResponseType(typeof(Error), StatusCodes.Status400BadRequest)]
[ProducesResponseType(typeof(Error), StatusCodes.Status500InternalServerError)]
public async Task<List<Response>> GetCodeAPIController
(Request request)
{
var response1 = await _interface1.GetCodesByFilter(request);
return await _interface2.GetGrowthRate(response1.Codes);
}
}
, meanwhile, in the first version of this controller when I don't have the first interface, I write my controller test like this and it works correctly:
public class MyAPIControllerTest : IntegrationBaseTest
{
public MyAPIControllerTest ()
: base(nameof(Repository))
{
DataProvider.InsertData<Data1>
(Context, nameof(Data1));
}
[Theory]
[InlineData("/api/MyAPI/CodeAPI")]
public async Task GetGrowthRate(string url)
{
var request = new Request()
{
Filter = new List<string>() { "111" }
};
var content = await HttpHandler.PostURI<Request>(url, request, Factory);
List<Response> response = JsonConvert.DeserializeObject<List<Response>>(content);
// Assert
Assert.NotNull(response);
Assert.Single(response);
Assert.Equal("111", response[0].CompanyNationalCode);
}
}