deno 测试应用程序代码中的嵌入式测试:为什么不呢?
deno testing embedded tests in application code: why not?
我今天尝试了一些东西,但我没有看到不这样做的足够缺点。
import {
verify,
create,
getNumericDate,
} from "https://deno.land/x/djwt@v2.2/mod.ts";
import { User } from "./models.ts";
import { getUser } from "./controllers/users.ts";
import * as bcrypt from "https://deno.land/x/bcrypt@v0.2.4/mod.ts";
const key = "some-secret-jey";
const header = { alg: "HS512", typ: "JWT" };
const payload = {
iss: "ted",
exp: getNumericDate(60 * 60),
};
export async function authenticate(username: string, password: string) {
const user: User = await getUser(username);
if (await passMatch(password, user.hash)) {
return await genToken();
}
}
async function passMatch(password: string, hash: string): Promise<boolean> {
return await bcrypt.compare(password, hash);
}
async function genToken(): Promise<string> {
const jwt = await create(
{ alg: "HS512", typ: "JWT" },
{ foo: "bar" },
"secret"
);
return jwt;
}
// ****************************************************************
// TESTS
// ****************************************************************
import {
assertEquals,
assert,
} from "https://deno.land/std@0.90.0/testing/asserts.ts";
Deno.test("authenticate", async () => {
const token = await authenticate("joe", "test1.d");
assert(token);
});
Deno.test("passMatch", async () => {
const password = "password";
const hash = await bcrypt.hash(password);
assert(await passMatch(password, hash));
});
Deno.test("genToken", async () => {
assert(await genToken());
});
这是独立工作的,我知道 deno test
显然希望测试是外部化的,我知道有干净的代码。
但这是否解决了其他问题,例如拥有大量文件 auth.ts auth_test.ts 等,开发人员轻松,减少模拟和注入等...
像这样继续进行直接单元测试的长期后果是什么?
经过一些使用后,我找到了不这样做的理由,除此之外不是设计。
如果我 server.ts
导入 user.ts
那么当我 运行 服务器测试时,它也会 运行 用户测试。
我放弃了这个思想实验
我今天尝试了一些东西,但我没有看到不这样做的足够缺点。
import {
verify,
create,
getNumericDate,
} from "https://deno.land/x/djwt@v2.2/mod.ts";
import { User } from "./models.ts";
import { getUser } from "./controllers/users.ts";
import * as bcrypt from "https://deno.land/x/bcrypt@v0.2.4/mod.ts";
const key = "some-secret-jey";
const header = { alg: "HS512", typ: "JWT" };
const payload = {
iss: "ted",
exp: getNumericDate(60 * 60),
};
export async function authenticate(username: string, password: string) {
const user: User = await getUser(username);
if (await passMatch(password, user.hash)) {
return await genToken();
}
}
async function passMatch(password: string, hash: string): Promise<boolean> {
return await bcrypt.compare(password, hash);
}
async function genToken(): Promise<string> {
const jwt = await create(
{ alg: "HS512", typ: "JWT" },
{ foo: "bar" },
"secret"
);
return jwt;
}
// ****************************************************************
// TESTS
// ****************************************************************
import {
assertEquals,
assert,
} from "https://deno.land/std@0.90.0/testing/asserts.ts";
Deno.test("authenticate", async () => {
const token = await authenticate("joe", "test1.d");
assert(token);
});
Deno.test("passMatch", async () => {
const password = "password";
const hash = await bcrypt.hash(password);
assert(await passMatch(password, hash));
});
Deno.test("genToken", async () => {
assert(await genToken());
});
这是独立工作的,我知道 deno test
显然希望测试是外部化的,我知道有干净的代码。
但这是否解决了其他问题,例如拥有大量文件 auth.ts auth_test.ts 等,开发人员轻松,减少模拟和注入等...
像这样继续进行直接单元测试的长期后果是什么?
经过一些使用后,我找到了不这样做的理由,除此之外不是设计。
如果我 server.ts
导入 user.ts
那么当我 运行 服务器测试时,它也会 运行 用户测试。
我放弃了这个思想实验