Is there a way to unit test or debug a web api in one vs solution? I am consuming the WebAPI using HttpClient and have two instances of VS up to do this.
in 1 VS instance I have the unit test, in the second vs instance I have the webapi running in localhost.
Is there a better way to do this?
Is the preferred way to unit test is to have a reference to the WebAPI project?
I want to consume it using httpClient and not have to reference it in the UnitTest Project.
so in my UnitTest method it would have a baseAddress of "http://localhost:1234"
this would be where the WebAPI if launched from the same solution would be hosted.
The current way I am debugging requires me to launch a second Visual Studio instance with the same solution loaded and have one running the WebAPI project, while the other Visual Studio runs the Unit Test project.
In the current version of ASP.Net Core the official solution to this is to use the Microsoft.AspNetCore.TestHost Nuget assembly which creates a simulated web server hosting the web project in the test project.
Details on usage can be found here: https://docs.microsoft.com/en-us/aspnet/core/testing/integration-testing
On the one hand it feels like a bit of a fudge because by what definition is it genuine integration testing if a real web server is not involved, on the other hand it does at least work pretty seamlessly once you get it set up.
I try the selft hosted but I get some problens 1.
IisExpress solution work for me very good
bathe file to deploy to iis
I work with Nunit frameWork
attach to iisexpress
Debug test then make a breakpoint goto Debug>Attach to process> in the attach to select click OK,
search iisexpress and click attach
You can self host the web api as mike mentioned,
for more details, http://www.asp.net/web-api/overview/older-versions/self-host-a-web-api
you could start the hosting when you initialize your unit test suite, and shutdown when you cleanup the test suite.
If you have an API project and you created an end-to-end unit test project for the API (meaning you are testing the API directly using HttpClient, WebClient, or other http client wrappers), you have to enable "Multiple startup projects" on the solution. Follow steps below:
So a colleague and I just tried the suggested answers to no avail. However, we have actually found a solution to this that works well by attaching to the IIS process once you are in debug mode on your test. Here are the steps:
For extra ease, we downloaded an extension for attaching to IIS automatically which gave us a menubar item in the Tools menu.
For superb ease we customized the toolbar section to add the menubar command to the toolbar so that attaching to IIS was one simple click.