如何运行一个虚假的ASP.NET Core Web API以进行测试目的?

huangapple go评论63阅读模式
英文:

How to run a fake ASP.NET Core Web API for testing purpouses?

问题

我有两个ASP.NET Core Web API项目,API-1和API-2。API-1通过new HttpClient().GetAsync("API-2 url")内部发送请求来使用API-2。我还有一个测试项目,在那个测试项目中,我想伪造API-2的响应来对API-1进行一些测试。那么,我如何在测试项目中运行API-2的伪装版本?

英文:

I have two ASP.NET Core Web API projects, API-1 and API-2. API-1 is using the API-2 by sending requests internally via new HttpClient().GetAsync("API-2 url") I have a test project too. In that test project, I want to fake API-2 responses to do some tests on API-1. So, how can I run a fake (mock) version of API-2 in the test project?

答案1

得分: 1

Moq框架对此非常有用。我用于处理这种情况的方法是将对api-2的调用封装在一个自定义客户端中,该客户端使用底层的HttpClient,基本上就像这样:

public class MyOwnHttpClient : IMyOwnHttpClient {

  readonly HttpClient _client;

  public MyOwnHttpClient() {
    _client = new HttpClient();
  }

  public async Task<MyApiResponse> MyApiCall() {
    return await _client.GetAsync("API-2 url");
  }
}
public interface IMyOwnHttpClient  {
 
  Task<MyApiResponse> MyApiCall();
}

然后,您可以在测试类中使用Moq来模拟您自己的客户端接口,并设置它返回您想要的任何内容。它还具有大量用于单元测试的其他有用功能。

[TestClass]
public class MyTestClass {

  private Mock<IMyOwnHttpClient> _client; // 我们的Api-2模拟

  private Api1Class _api1Class; // 使用客户端并在此处进行测试的类

  [TestMethod]
  public void Test() {
    _client.Setup(x => x.MyApiCall()).Returns(Task.FromResult(new MyApiResponse()));

    _api1Class = new Api1Class(_client.Object);
    _api1Class.MethodCall(); // 调用使用客户端的方法

    _client.Verify(x => x.MyApiCall(), Times.Once);
  }
}

顺便说一下,这也适用于您昨天的问题。您只需设置它返回一个带有您想要的错误代码的HtppResponse对象。

希望这有所帮助。如果有任何不清楚的地方,请告诉我。

英文:

The Moq Framework is very useful for this. An approach that I use for cases like this is to abstract the calls to api-2 in a custom client that uses an underlying HttpClient. Basically something like this:

public class MyOwnHttpClient : IMyOwnHttpClient {

  readonly HttpClient _client;

  public MyOwnHttpClient() {
    _client = new HttpClient();
  }

  public async foo MyApiCall() {
    return await _client.GetAsync(&quot;API-2 url&quot;);
  }
}
public interface IMyOwnHttpClient  {
 
  foo MyApiCall();
}

You can then use Moq in the test class to mock your own client interface and set it up to return whatever you want. It also has a ton of other useful features for unittesting.

[TestClass]
public class MyTestClass {

  private Mock&lt;IMyOwnHttpClient&gt; _client; // Our Api-2 Mock

  private Api1Class _api1Class; // Class that is using the client and is being tested here

  [TestMethod]
  public void Test() {
    _client.Setup(x =&gt; x.MyApiCall()).Returns(new foo);

    _api1Class = new Api1Class(_client.Object);
    _api1Class.MethodCall(); // Calling the method that uses the client

    _client.Verify(x =&gt; x.MyApiCall(), Times.Once);
  }
}

By the way, this should also work for your question from yesterday. You can just set it up to return a HtppResponse object with the error code you want.

Hope this helps. Let me know if anything is unclear.

huangapple
  • 本文由 发表于 2023年7月6日 13:17:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76625709.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定