微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

sinon 测试 azure containerclient.listblobsbyhierarchy 的问题

如何解决sinon 测试 azure containerclient.listblobsbyhierarchy 的问题

我有以下其余端点代码“/files/lookup”。这将接收一个查询参数 folderPath,并将返回一个包含详细信息(包括元数据)但不包含内容文件列表。 我包括了其余端点的内容。这会连接到 azure blob 存储。

@get('/files/lookup',{ ... })
...
const blobServiceClient: BlobServiceClient = BlobServiceClient.fromConnectionString(
        this.azureStorageConnectionString,);
    const containerClient: ContainerClient = blobServiceClient.getContainerClient(container);
    const filesPropertiesList: FileProps[] = [];
 try {
        for await (const item of containerClient.listBlobsByHierarchy('/',{
            prefix: decodedAzureFolderPath,includeMetadata: true,})) {
            if (item.kind !== 'prefix') {
                const blobitem: BlobItem = item;
                const blobProperties: BlobProperties = blobitem.properties;
                const blobMetadata: Record<string,string> | undefined = blobitem.Metadata;

                const aFileProperties: FileProps = {
                    name: item?.name,uploadedDate:
                        blobProperties.lastModified?.toISOString() ?? blobProperties.createdOn?.toISOString(),size: blobProperties.contentLength,contentType: blobProperties.contentType,Metadata: blobMetadata,};
                filesPropertiesList.push(aFileProperties);
            }
        }
    } catch (error) {
        if (error.statusCode === 404) {
            throw new HttpErrors.NotFound('Retrieval of list of files has Failed');
        }
        throw error;
    } 
   return filesPropertiesList;

我正在做 sinon 测试。我是sinon的新手。我无法有效地使用模拟/存根/等。测试返回具有属性文件列表的端点。无法模拟/存根容器客户端的 listBlobsByHierarchy 方法

describe('GET /files/lookup',() => {
    let blobServiceClientStub: sinon.SinonStubbedInstance<BlobServiceClient>;
    let fromConnectionStringStub: sinon.SinonStub<[string,StoragePipelineOptions?],BlobServiceClient>;
    let containerStub: sinon.SinonStubbedInstance<ContainerClient>;

    beforeEach(async () => {
        blobServiceClientStub = sinon.createStubInstance(BlobServiceClient);
        fromConnectionStringStub = sinon
            .stub(BlobServiceClient,'fromConnectionString')
            .returns((blobServiceClientStub as unkNown) as BlobServiceClient);

        containerStub = sinon.createStubInstance(ContainerClient);
        blobServiceClientStub.getContainerClient.returns((containerStub as unkNown) as ContainerClient);
    });

    afterEach(async () => {
        fromConnectionStringStub.restore();
    });

    it('lookup for files from storage',async () => {
        /*             let items: PagedAsyncIterableIterator<({ kind: "prefix"; } & BlobPrefix) | ({ kind: "blob"; } & BlobItem),ContainerListBlobHierarchySegmentResponse>;

                    sinon.stub(containerStub,"listBlobsByHierarchy").withArgs('/',{ prefix: "myf/entity/172/",includeMetadata: true }).returns(items);
                    const response = await client.get(`/files/lookup?folderpath=myf%2Fentity%2F172%2F`).expect(200); */
    });
    
});

解决方法

由于我没有找到任何方法来模拟具有相同类型的此方法的返回,因此我选择了类型“any”。由于我是这方面的新手,让我的头脑做到这一点真的很有挑战性!

it('lookup for files from storage',async () => {
        /* eslint-disable @typescript-eslint/naming-convention */
        const obj: any = [
            {
                kind: 'blob',name: 'myf/entity/172/0670fdf8-db47-11eb-8d19-0242ac13000.docx',properties: {
                    createdOn: new Date('2020-01-03T16:27:32Z'),lastModified: new Date('2020-01-03T16:27:32Z'),contentLength: 11980,contentType: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',},metadata: {
                    object_name: 'Testing.docx',category: 'entity',reference: '172',object_id: '0670fdf8-db47-11eb-8d19-0242ac13000',];
        containerStub.listBlobsByHierarchy.returns(obj);

        const actualResponse = await (await client.get('/files/lookup?folderpath=myf/entity/172')).body;
        const expectedResponse: any[] = [ WHATEVER ]
        expect(actualResponse).deepEqual(expectedResponse);
    });

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?