如何在一个解决方案中对webAPi进行调试/单元测试 [英] How to debug/unit test webAPi in one solution

查看:477
本文介绍了如何在一个解决方案中对webAPi进行调试/单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以在一个vs解决方案中对Web api进行单元测试或调试?我正在使用HttpClient来使用WebAPI,并且有两个VS实例可以做到这一点.

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.

在1个VS实例中,我进行了单元测试,在第二个vs实例中,我在本地主机上运行了webapi.

in 1 VS instance I have the unit test, in the second vs instance I have the webapi running in localhost.

有更好的方法吗?

单元测试的首选方法是引用WebAPI项目吗?

Is the preferred way to unit test is to have a reference to the WebAPI project?

我想使用httpClient来使用它,而不必在UnitTest项目中引用它.

I want to consume it using httpClient and not have to reference it in the UnitTest Project.

因此在我的UnitTest方法中,它的baseAddress为" http://localhost:1234 "

so in my UnitTest method it would have a baseAddress of "http://localhost:1234"

如果从同一解决方案启动,则将在此处托管WebAPI.

this would be where the WebAPI if launched from the same solution would be hosted.

当前的调试方式要求我启动另一个加载了相同解决方案的Visual Studio实例,并让其中一个运行WebAPI项目,而另一个Visual Studio运行单元测试项目.

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.

推荐答案

  1. 开始调试单元测试
  2. 在测试代码的第一行,或者在调用本地Web api项目之前
  3. 右键单击您的Web api项目,然后依次单击调试">启动新实例"

这篇关于如何在一个解决方案中对webAPi进行调试/单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆