Let us say I want to perform an integration test on an API controller method looking like this:
public async Task<IActionResult> Get(Guid id)
{
try
{
if (id == Guid.Empty)
{
return new BadRequestObjectResult("Id is not valid");
}
var result = await _fileStorageService.GetFileUrlWithAccessKey(id);
if (result == null)
{
return new NotFoundObjectResult("Could not find any file with given id");
}
var document = new Document()
{
Url = result
};
return Ok(document);
}
catch (StorageException storageException)
{
switch (storageException.RequestInformation.HttpStatusCode)
{
case 404:
return Json(StatusCode(404));
default:
return Json(StatusCode(500));
}
}
catch (Exception)
{
return Json(StatusCode(500));
}
}
My integration test looks like this(I have just started to implement it, the first test is not fully completed):
public class DocumentsControllerTest : IClassFixture<TestServerFixture>
{
private readonly HttpClient Client;
public DocumentsControllerTest(TestServerFixture fixture)
{
Client = fixture.Client;
}
[Fact]
public async Task Get_WhenCalledNotExistingFileId_ShouldReturn404StatusCode()
{
var nonExistentId = Guid.NewGuid();
var response = await Client.GetAsync($"/documents/{nonExistentId}");
}
}
In the API controller method I want to mock out the call to _fileStorageService.GetFileUrlWithAccessKey(id);
I have tried to mock out the call to __fileStorageService, by mocking out the interface IFileStorageService
public class TestServerFixture
{
/// <summary>
/// Test fixture that can be used by test classes where we want an HttpClient
/// that can be shared across all tests in that class.
/// </summary>
public HttpClient Client { get; set; }
private readonly TestServer _server;
public TestServerFixture()
{
var webHostBuilder = new WebHostBuilder()
.UseEnvironment("UnitTest")
.UseStartup<Startup>()
.ConfigureServices(services =>
{
services.TryAddScoped(serviceProvider => A.Fake<IFileStorageService>());
});
_server = new TestServer(webHostBuilder);
Client = _server.CreateClient();
}
public void Dispose()
{
Client.Dispose();
_server.Dispose();
}
}
But I dont think the call to var result = await _fileStorageService.GetFileUrlWithAccessKey(id);
is being mocked out correct in my TestServerFixture
class, because my test code keep going into this code and I am getting error because I have not provided parameters to fileStorageService. What can I do in this scenario to mock out the call to a service completely, so we dont go into that code?