gpt4 book ai didi

unit-testing - 这是否遵循 AAA 模式?

转载 作者:行者123 更新时间:2023-12-02 07:47:22 24 4
gpt4 key购买 nike

我有以下测试:

[Test]
public void VerifyThat_WhenHasInterceptorIsCalledWithAComponentModelThatHasTheLogAttribute_TheReturnValueIsTrue()
{
// Arrange
Mock<ComponentModel> mock = /* ... */;
LoggingInterceptorsSelector selector = new LoggingInterceptorsSelector();
// Act & Assert togheter
Assert.That(selector.HasInterceptors(mock.Object), Is.True);
}

统一Act & Assert有什么问题吗?
如果错误应该如何解决?
编辑:
这种测试呢:

[Test]
[Category("HasInterceptors() Tests")]
public void VerifyThat_WhenHasInterceptorsIsCalledWithANullComponentModel_AnArgumentNullExceptionIsThrown()
{
LoggingModelInterceptorsSelector selector = new LoggingModelInterceptorsSelector();

Assert.That(new TestDelegate(() => selector.HasInterceptors(null)), Throws.TypeOf<ArgumentNullException>());
}

act 和 assert 必须在同一行才能正确断言。至少我是这么理解的。

这个怎么样:

[Test]
[Category("HasInterceptors() Tests")]
public void VerifyThat_WhenHasInterceptorsIsCalledWithANullComponentModel_AnArgumentNullExceptionIsThrown()
{
LoggingModelInterceptorsSelector selector = new LoggingModelInterceptorsSelector();
var testDelegate = new TestDelegate(() => selector.HasInterceptors(null));
Assert.That(testDelegate, Throws.TypeOf<ArgumentNullException>());
}

这是否更好地遵守 AAA 模式?

最佳答案

我会这样做:

[Test]
public void VerifyThat_WhenHasInterceptorIsCalledWithAComponentModelThatHasTheLogAttribute_TheReturnValueIsTrue()
{
// Arrange
Mock<ComponentModel> mock = /* ... */;
LoggingInterceptorsSelector selector = new LoggingInterceptorsSelector();

// Act
var result = selector.HasInterceptors(mock.Object);

// Assert
Assert.That(result, Is.True);
}

AAA 级且易于阅读。

关于unit-testing - 这是否遵循 AAA 模式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5977806/

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