单元测试的三个A
测试是软件构建的重要组成部分。对于生产就绪的应用程序,我们需要拥有可靠且经过充分测试的代码,并且尽可能减少错误。有很多方法可以用来测试代码。在本文中,我将介绍最流行的方法之一——单元测试。单元测试涉及对应用程序中的特定模块或代码片段进行测试。编写测试时,您可能需要遵循某种模式来编写结构良好、可读性强的测试。这就是 AAA 模式的用武之地。AAA 代表“安排 (Arrange)、执行 (Act) 和断言 (Assert)”。这是一种确保我们涵盖代码模块测试各个方面的好方法。
安排数据状态以进行测试。
通过某种方法对数据执行某种操作。
断言根据该数据采取行动的结果正是我们所期望的。
这是在任何测试框架中使用 AAA 模式的基本流程。为了通过代码示例逐一讲解,我们将使用 JavaScript 的Jasmine测试框架。如果您之前没有听说过 Jasmine,它与您可能熟悉的其他测试框架(例如 RSpec 和 JSpec)类似。现在,让我们开始编写一些测试吧!
实现 AAA 模式
在本例中,我们将用 JavaScript 测试一个 User 模型。User 类的构造函数将接收一个全名对象,用于设置其名字、中间名首字母和姓氏属性。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
class User { | |
constructor(inputData){ | |
this.firstName = inputData.firstName; | |
this.lastName = inputData.lastName; | |
this.middle = inputData.middle; | |
} | |
getFullName() { | |
return `${this.firstName} ${this.middle}. ${this.lastName}`; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
class User { | |
constructor(inputData){ | |
this.firstName = inputData.firstName; | |
this.lastName = inputData.lastName; | |
this.middle = inputData.middle; | |
} | |
getFullName() { | |
return `${this.firstName} ${this.middle}. ${this.lastName}`; | |
} | |
} |
我们的 User 类包含一个方法getFullName()
,该方法应该返回用户的全名。那么,我们如何检查这个方法是否按预期执行呢?我们可以编写一个单元测试来确保获取到正确的值。下面的代码就是这样做的!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
// Test Suite | |
describe(`${User.name} Class`, () => { | |
it('getFullName() returns users full name', () => { | |
// arrange | |
const user = new User({firstName: "Jay", middle: "J", lastName: "Cruz"}); | |
// act | |
const result = user.getFullName(); | |
// assert | |
expect(result).toBe(`${user.firstName} ${user.middle}. ${user.lastName}`); | |
}); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
// Test Suite | |
describe(`${User.name} Class`, () => { | |
it('getFullName() returns users full name', () => { | |
// arrange | |
const user = new User({firstName: "Jay", middle: "J", lastName: "Cruz"}); | |
// act | |
const result = user.getFullName(); | |
// assert | |
expect(result).toBe(`${user.firstName} ${user.middle}. ${user.lastName}`); | |
}); | |
}); |
因此,我们测试套件的第一部分是
describe
方法。它将describe
我们要测试的代码组合在一起。接下来it
的测试部分会说明这段特定的代码应该做什么。在本例中,它应该返回全名。在测试主体内部,it
我们实现了安排、执行和断言,并赋予每个部分特定的职责。安排会创建一个 User 类的新实例,而执行会使用我们正在测试的方法执行操作getFullName()
。断言会确保对用户调用的评估结果是否getFullName()
正是我们需要的。
结论
AAA 模式为我们提供了简单但有效的代码测试步骤。该模式的每个步骤都有各自的任务。“arrange”步骤设置数据,“act”步骤执行测试所需的操作,“assert”步骤则判断对数据执行操作的结果是否符合我们的预期。
鏂囩珷鏉ユ簮锛�https://dev.to/coderjay06/the- Three-as-of-unit-testing-b22