单元测试 AAA 模式中的数据提取
在“AAA”模式中,应将行为数据的提取写入何处?
在 Act 部分还是 Assert 部分?
考虑这个单元测试,两个人的提取,应该像示例中那样在 Act 中还是在 Assert 中?我们想为公司所有的 UT 制定一个标准。
[Test]
public void Test()
{
// Arrange
var p1 = new Person();
var p2 = new Person();
Session.Save(p1);
Session.Save(p2);
// Act
var result = new PersonQuery().GetAll();
var firstPerson = result[0];
var secondPerson = result[1];
// Assert
Assert.AreEqual(p1.Id, firstPerson.Id);
Assert.AreEqual(p2.Id, secondPerson.Id);
}
(请忽略在这个简单的测试中我可以编写 Assert.AreEqual(p1.Id, result[0].Id);
)
我知道这不是一个大问题,但我仍然想知道如何把事情做得最好。
In "AAA" pattern where extraction of the act data should be written?
In the Act or in the Assert section?
Consider this Unit Test, the extraction of the two persons, should it be in the Act as in the example or in the Assert? we would like to make a standard for all of our UT in the company.
[Test]
public void Test()
{
// Arrange
var p1 = new Person();
var p2 = new Person();
Session.Save(p1);
Session.Save(p2);
// Act
var result = new PersonQuery().GetAll();
var firstPerson = result[0];
var secondPerson = result[1];
// Assert
Assert.AreEqual(p1.Id, firstPerson.Id);
Assert.AreEqual(p2.Id, secondPerson.Id);
}
(please ignore that in this simple test I can write Assert.AreEqual(p1.Id, result[0].Id);
)
I know it's not a huge problem, but I still want to know how to do things the best.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该发生在断言阶段:
Act
阶段仅涉及调用被测试的方法。This should happen in the Assert phase:
The
Act
phase only involves invoking the method under test.这取决于经验法则 - 行动阶段代表测试下业务逻辑的执行。在您的情况下,这取决于提取是否影响任何业务逻辑,如果
result[i]
索引器是简单的集合项访问器 - 它不是Act
,因为您已经将数据提取到result
变量,否则 - 它将是Act
。It depends, rule of thumb - Act stage represents execution of business logic under the test. In your case it depends on whether extraction affects any business logic, if
result[i]
indexer is straightforward collection item accesor - it is notAct
since you alreay extracted data into theresult
variable, otherwise - it would beAct
.