【问题标题】:how to create integration test for repository using xunit and microservices如何使用 xunit 和微服务为存储库创建集成测试
【发布时间】:2021-10-27 21:15:13
【问题描述】:

我真的迷失在集成测试单元主题中,在我的微服务中,我有一个从 azure bus 获取数据的类,它有一个将数据存储在数据库中的方法,我需要为它编写一个集成测试,什么到目前为止我已经完成了:

 public class FxRateRepositoryTest
{
    public IFxRateRepository fxraterepo;
    public FxRateRepositoryTest(IFxRateRepository fxraterepo) => this.fxraterepo = fxraterepo;
    [Fact]
    public void StoreFxRatesAsync_True()
    {
        var newrate = new List<Rate> {

            new Rate
            {
                CurrencyPair="HHGHGH",
                MidRate=1,
                RateTimestampInUtc=DateTime.UtcNow.ToString()
            }
        };

        var newFx = new FXRefreshResponseDTO()
        {
            Rates = newrate
        };

        var rslt = this.fxraterepo.StoreFxRatesAsync(newFx);
        Assert.True(rslt.Result);

    }
}

我得到一个错误xunit构造函数参数异常没有匹配的夹具数据,我搜索了一下错误,结果我需要添加类似的东西:

   public class FxRateRepositoryTest : IClassFixture<CustomWebApplicationFactory<Startup>>

说实话,我不知道是什么,但我仍然遇到同样的错误,知道吗?非常感谢您的帮助

【问题讨论】:

    标签: c# asp.net-core integration-testing


    【解决方案1】:

    当然,您必须让应用运行并配置接口实现 (IOC)。

    因此,创建一个继承 WebApplicationFactory&lt;Startup&gt; 形式的基类。 startup 类必须是您用于启动 webapi 的类(例如)。

    那么,最好传递一个新的 appconfig.json,这样您就可以为您的集成测试设置一些自定义配置。

    它会变成类似:public class BaseIntegrationTests : WebApplicationFactory&lt;Startup&gt;

    然后使用 IClassFixture 将此基类继承到您的测试类。类似于:公共类 PaymentGets:IClassFixture

    看看这段代码,还有一些新想法的输入:https://github.com/j-ew-s/study-payment-gateway/blob/main/Study.PaymentGateway.IntegrationTests/Base/BaseIntegrationTests.cs

    【讨论】:

      猜你喜欢
      • 2016-01-25
      • 2019-03-22
      • 2019-11-02
      • 2017-07-25
      • 2022-01-07
      • 2018-11-24
      • 2020-03-08
      • 2019-02-18
      • 2017-04-15
      相关资源
      最近更新 更多