How to debug/unit test webAPi in one solution

前端 未结 7 1980
借酒劲吻你
借酒劲吻你 2021-02-12 22:17

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 instan

7条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-12 22:40

    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:

    1. Ensure your unit-test project and Web API project are in the same solution.
    2. Ensure your solution is set single-startup in the properties section (i.e. leave as default).
    3. Add a breakpoint to the endpoint in your controller method that you want to debug.
    4. Add a breakpoint in your test that you want to debug and then right-click and hit "Debug Tests" (or use your favorite Test Runner like ReSharper to debug).
    5. Once your break point hits, click Debug | Attach to Process...
    6. Find and click on your local IIS process for the given service, then hit Attach
    7. Continue debugging and watch your breakpoint in your service get hit

    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.

提交回复
热议问题