【问题标题】:Sequence contains no elements being thrown only from AutoFixture序列不包含仅从 AutoFixture 抛出的元素
【发布时间】:2016-06-27 21:41:22
【问题描述】:

我在 BDD 测试中使用 AutoFixture。

我正在尝试为 User 类编写一个夹具,该类又使用 CentralConfiguration 类。 CentralConfiguration 构造函数如下所示:

public CentralConfiguration(
    IConfigurationRepository configurationRepository,
    ILogger logger)
{
   _logger = logger;
   _configuration = configurationRepository.Single();
   LogPropertyValues();
}

构造函数中的第二行,虽然在用户使用时工作正常,但抛出“序列不包含元素”异常每次我尝试构建一个测试夹具。我什至尝试手动构建Configuration 对象并使用

configuration.Single().Returns(myCustomObject)

但没有任何改变(实际上这一行开始抛出相同的异常)。

我做错了什么,我该如何规避这个问题?

【问题讨论】:

  • 您是否也在使用自动模拟 Glue 库? (例如 AutoFixture.AutoMoq)
  • 如果CentralConfiguration 只依赖于一个Whatchamacallit,那你为什么不注入that呢? Injection Constructors should be simple.

标签: c# .net exception testing autofixture


【解决方案1】:

CentralConfiguration 更改为:

public CentralConfiguration(
    IConfiguration configuration,
    ILogger logger)
{
   _logger = logger;
   _configuration = configuration;
   LogPropertyValues();
}

还可以考虑从构造函数中删除对LogPropertyValues 的调用。 Injection Constructors should be simple.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-25
    • 1970-01-01
    • 1970-01-01
    • 2021-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多