.net core 2.1 test API controller missing AspNetCore.Mvc reference
.net core 2.1 test API controller missing AspNetCore.Mvc reference
I'm trying to tests an API controller with .net core 2.1 as from this MS doc.
My code looks like
[Fact]
public async Task ShouldReturnOkWithHealthBackend()
{
var apiConsumerService = new Mock<IApiConsumerService>();
apiConsumerService.Setup(m => m.Consume()).Returns(Task.FromResult(true));
var controller = new HealthController(apiConsumerService.Object);
await controller.GetHealth();
}
When doing the await controller method invocation I get stucked with an "Assembly not referenced error"
I can add the
using Microsoft.AspNetCore.Mvc;
But VS says that AspNetCore does not exists in the Microsoft namespace.
Intellisense suggests me to add some references but this doesn't work too.
But clicking it does nothing. I have also tried to play with the csproj file to see if I can force those assemblies to be loaded but still no success.
I've tested with the XUnit template as well as a custom NUnit project with same results.
My csproj from API and test project are
What kind of project do I need to create to test a Controller like in the MS doc?
var result = await controller.GetHealth();
– joey
1 hour ago
see docs.microsoft.com/en-us/aspnet/core/mvc/controllers/…
– joey
1 hour ago
I'm aware that is still missing the Assert part, but it is irrelevant for this as I cannot invoke any controller method. @joey the docs you're linking are the same I'm refrencing in my question. I haven't been able to see any particular requirement for the solution.
– guillem
1 hour ago
what is the result after assignign the result to a variable?
– joey
1 hour ago
1 Answer
1
try adding
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.1" />
to the test project, restore package in test project, see if that solves.
Also, I noticed you didnt bind your project containing the controllers to a version, you should instead do so with the following element.
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.1" />
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You should be assigning the result to a variable, to start. Then to Assert
– joey
1 hour ago