我尝试用S3编写单元测试,但是由于错误而失败。
我输入了打字稿。
测试方法
export const getUrlFromS3 = async (fileName: string): Promise<string> => {
const s3: S3 = new S3({
accessKeyId: process.env.AWS_ACCESS_KEY,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
});
const params = {
Bucket: process.env.AWS_BUCKET_NAME,
Key: `${fileName}.pdf`,
Expires: 60 * 5
};
return new Promise((resolve, reject) => {
s3.getSignedUrl("getObject", params, (err: Error, url: string) => {
if (err) {
reject(err);
}
resolve(url);
});
});
};
和我的测试文件
import * as manageS3 from "./manageS3";
import * as Aws from "aws-sdk";
import * as Sinon from "sinon";
import { expect } from "chai";
describe("manageS3", () => {
let sandbox: Sinon.SinonSandbox;
beforeEach(() => (sandbox = Sinon.createSandbox()));
afterEach(() => sandbox.reset());
it("getUrlFromS3 test", async () => {
sandbox
.stub(Aws.S3.prototype, "getSignedUrl")
.callsFake((operation, params, callback) => {
return callback(undefined, "");
});
const url = await manageS3.getUrlFromS3("");
console.log(url);
});
});
,它是错误消息
Argument of type '(operation: any, params: any, callback: any) => any' is not assignable to parameter of type '(operation: string, params: any) => string'.ts(2345)
S3有两种方法
getSignedUrl(operation: string, params: any, callback: (err: Error, url: string) => void): void;
getSignedUrl(operation: string, params: any): string;
为什么第一个方法不能存根?