gpt4 book ai didi

c# - 对 Autofac 模块进行单元测试以达到 100% 的代码覆盖率

转载 作者:太空狗 更新时间:2023-10-29 20:04:21 25 4
gpt4 key购买 nike

我们有一个进行复杂计算的核心库,我们认为它很关键,我们希望该库的代码覆盖率为 100%。我们现在有 96%,这很好,但是因为这个类,我们无法达到 100%:

public class IoCModule : Autofac.Module
{
protected override void Load(Autofac.ContainerBuilder builder)
{
builder.RegisterType<SomeMathServiceA>().As<ISomeMathServiceA>();
builder.RegisterType<SomeMathServiceB>().As<ISomeMathServiceB>();

//... more registrations
}
}

我不知道如何测试它,或者我们是否真的需要测试它。

我已经尝试了一个单元测试,它采用这个模块并创建一个 IContainer 并解析每个寄存器依赖性,但是一些服务访问数据库和配置文件,在这种情况下模拟起来非常复杂。

完成!!!! enter image description here

最佳答案

免责声明:类级单元测试不被认为是明智的

(作者)

我想您所说的单元测试是指单元是一个类的“类级单元测试”。如果你想测试 IoCModule您应该使用组件/库级测试来测试整个库是否正常工作。这(应该)包括 IoCModule - 以及图书馆里的所有其他东西。使用此级别的测试通常无法达到 100% 的分支覆盖率,但此级别的测试 + 类级别单元测试的组合可以实现非常好的测试可靠性。我还想说达到 80% 的综合覆盖率比只进行类级单元测试要好。虽然每个类(class)本身都可以完全按照测试工作,但整体可能无法按预期工作。这就是您应该执行组件级测试的原因。

如何验证一个类型是否被注册:

现在,如果您仍然坚持执行测试,请不要再犹豫了,您可以这样做:

public class MyModuleTest
{
private IContainer container;

[TestFixtureSetUp]
public void TestFixtureSetUp()
{
var containerBuilder = new ContainerBuilder();

// register module to test
containerBuilder.RegisterModule<MyModule>();

// don't start startable components -
// we don't need them to start for the unit test
this.container = containerBuilder.Build(
ContainerBuildOptions.IgnoreStartableComponents);
}

[TestCaseSource(typeof(TypesExpectedToBeRegisteredTestCaseSource))]
public void ShouldHaveRegistered(Type type)
{
this.container.IsRegistered(type).Should().BeTrue();
}

[TestFixtureTearDown]
public void TestFixtureTearDown()
{
this.container.Dispose();
}

private class TypesExpectedToBeRegisteredTestCaseSource : IEnumerable<object[]>
{
private IEnumerable<Type> Types()
{
// whatever types you're registering..
yield return typeof(string);
yield return typeof(int);
yield return typeof(float);
}

public IEnumerator<object[]> GetEnumerator()
{
return this.Types()
.Select(type => new object[] { type })
.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}

这给出了如下测试输出: enter image description here

因此每种类型都单独报告。

哇,这很简单 - 那又是什么问题呢?

现在在上面的例子中你可以看到 single 的测试(= float ) 正在过去。现在看看模块:

public class MyModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType<float>();
}
}

当我们实际尝试解析 float 时通过:

container.Resolve<float>();

是这样的:

Autofac.Core.DependencyResolutionException : No constructors on type 'System.Single' can be found with the constructor finder 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder'.

当然,我们可以调整测试以执行 Resolve(Type t)而不是使用 IsRegistered(Type t) - 然而,还有很多其他方法可以使测试通过 - 但实现失败。例如:

  • 绑定(bind)类型以使用builder.RegisterInstance<IFoo>(null)
  • 更改生命周期/范围,使其不再正常工作。

关于c# - 对 Autofac 模块进行单元测试以达到 100% 的代码覆盖率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35862278/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com