单元测试数据库查询
Unit Testing Database Queries
问题: 作为单元测试的初学者,我最近 运行 遇到的一个问题是处理 类 具有大量数据库的问题用法。
使用的技术: Typescript、SinonJS、Node-Postgres 和 Jest
示例: 对于给定的函数
public async insertUserDetails() {
const confirmationCode = this.generateConfirmationCode();
try {
await this.db.query({
text: 'INSERT INTO users (firstname, lastname, email, password, registration_date, confirmation_code) VALUES (, , , , , ) RETURNING *',
values: [this.firstname, this.lastname, this.email, this.password, this.registrationDate, confirmationCode]
});
} catch (error) {
throw error;
}
}
问题:
合适的单元测试是否是确保使用正确的 sql 调用 db.query() 函数,如下所示?或者这会被认为是浪费时间和 brittle 测试吗?
test("should format correct sql", async () => {
//Test Setup
const user = new User({
firstname: "John",
lastname: "doe",
email: "email@example.com",
password: "some_password_hash",
registrationDate: "some_random_date",
});
//Mocks
const queryStub = sinon.stub(user.db, "query");
const confirmationCode = sinon.stub(user,'generateConfirmationCode').returns("48fsakb8a-cfjdab");
const sampleQuery = {
text: 'INSERT INTO users (firstname, lastname, email, password, registration_date, confirmation_code) VALUES (, , , , , ) RETURNING *',
values: [user.firstname, user.lastname, user.email, user.password, user.registrationDate, confirmationCode]
}
//Preform
await user.insertUserDetails();
//Assert
sinon.assert.calledWith(queryStub, sampleQuery);
});
或者我应该跳过对这些类型的函数的所有单元测试和 类 并设置一个集成测试数据库并通过集成测试测试这些用例吗?
这是一个非常简单的示例,因为我没有测试数据库事务,但是测试事务变成了一个更大的蠕虫病毒库,因为要执行多个查询。
感谢您的帮助!这让我发疯 -__-
Would a suitable unit test be to make sure that the db.query()
function is called with the correct sql such as the following?
如果您专注于制作 db.query,那么您尝试做的不是单元测试。然后它变成了一个集成测试。您要测试的单位是 insertUserDetails
。所以你关注这个函数的行为。您可以测试的是: 1. db.query 是用正确的参数调用的(希望 this.db 可以被模拟) 2. 如果 db.query 抛出异常,那么您的 catch 块将被执行并且您的异常被抛出。
同样,您应该尝试为 this.db
class 编写覆盖 query
函数的单元测试。
因此,一旦您确定两个单元都已正确覆盖,那么这将是一个好的开始。
现在,如果您可以选择一次编写集成测试,您可以在其中使用某种内存数据库,或者可以是您可以随意使用的真实数据库,那么继续这样做,您不能编写完全单元测试。
希望这对您有所帮助。
问题: 作为单元测试的初学者,我最近 运行 遇到的一个问题是处理 类 具有大量数据库的问题用法。
使用的技术: Typescript、SinonJS、Node-Postgres 和 Jest
示例: 对于给定的函数
public async insertUserDetails() {
const confirmationCode = this.generateConfirmationCode();
try {
await this.db.query({
text: 'INSERT INTO users (firstname, lastname, email, password, registration_date, confirmation_code) VALUES (, , , , , ) RETURNING *',
values: [this.firstname, this.lastname, this.email, this.password, this.registrationDate, confirmationCode]
});
} catch (error) {
throw error;
}
}
问题: 合适的单元测试是否是确保使用正确的 sql 调用 db.query() 函数,如下所示?或者这会被认为是浪费时间和 brittle 测试吗?
test("should format correct sql", async () => {
//Test Setup
const user = new User({
firstname: "John",
lastname: "doe",
email: "email@example.com",
password: "some_password_hash",
registrationDate: "some_random_date",
});
//Mocks
const queryStub = sinon.stub(user.db, "query");
const confirmationCode = sinon.stub(user,'generateConfirmationCode').returns("48fsakb8a-cfjdab");
const sampleQuery = {
text: 'INSERT INTO users (firstname, lastname, email, password, registration_date, confirmation_code) VALUES (, , , , , ) RETURNING *',
values: [user.firstname, user.lastname, user.email, user.password, user.registrationDate, confirmationCode]
}
//Preform
await user.insertUserDetails();
//Assert
sinon.assert.calledWith(queryStub, sampleQuery);
});
或者我应该跳过对这些类型的函数的所有单元测试和 类 并设置一个集成测试数据库并通过集成测试测试这些用例吗?
这是一个非常简单的示例,因为我没有测试数据库事务,但是测试事务变成了一个更大的蠕虫病毒库,因为要执行多个查询。
感谢您的帮助!这让我发疯 -__-
Would a suitable unit test be to make sure that the db.query() function is called with the correct sql such as the following?
如果您专注于制作 db.query,那么您尝试做的不是单元测试。然后它变成了一个集成测试。您要测试的单位是 insertUserDetails
。所以你关注这个函数的行为。您可以测试的是: 1. db.query 是用正确的参数调用的(希望 this.db 可以被模拟) 2. 如果 db.query 抛出异常,那么您的 catch 块将被执行并且您的异常被抛出。
同样,您应该尝试为 this.db
class 编写覆盖 query
函数的单元测试。
因此,一旦您确定两个单元都已正确覆盖,那么这将是一个好的开始。
现在,如果您可以选择一次编写集成测试,您可以在其中使用某种内存数据库,或者可以是您可以随意使用的真实数据库,那么继续这样做,您不能编写完全单元测试。
希望这对您有所帮助。