【问题标题】:TypeError: this.dynamodb.batchGetItem is not a function with jestTypeError:this.dynamodb.batchGetItem 不是开玩笑的函数
【发布时间】:2021-06-25 17:49:35
【问题描述】:

我正在尝试创建一个简单的类来包装 Amazon SDK for dynamodb。测试时出现错误:

TypeError: this.dynamodb.batchGetItem is not a function

这是我的测试用例:test.js

import DynamoDBContentAccess from "../../../src/api/dynamodb_content_access";
const AWS = require("aws-sdk");
describe("services", () => {
  it("returns a list of services", async () => {
    let serviceInterface = new DynamoDBContentAccess();
    const params = {
      bearerToken: "fake bearer token",
      filter: {},
      skip: 10,
      limit: 10,
    };
    const mockResponse = {
      Responses: {
        Services: [
          {
            ServiceId: {
              S: "example1",
            },
          },
        ],
      },
    }
    const batchGetItemPromise = jest.fn().mockReturnValue({
      promise: jest.fn().mockResolvedValue(mockResponse),
    });
    serviceInterface.dynamodb = jest.fn().mockImplementation(() => { return {
      batchGetItem: batchGetItemPromise,
    }});
    const result = await serviceInterface.findServices(params);
    expect(result).toEqual(mockResponse);
  });
});

这是被测试的类:dynamodb_content_access.js

import ContentAccess from "./content_access"
const AWS = require("aws-sdk");
class DynamoDBContentAccess {
  constructor() {
   this.dynamodb = new AWS.DynamoDB();
  }
  async findServices (bearerToken, filter, skip, limit) {
    const params = {
      TableName: 'Services',
    };
    const results = await this.dynamodb.batchGetItem(params).promise();
    return results
    }
 }
 export default DynamoDBContentAccess;

我的模拟是否处于错误的级别?我错过了什么?

【问题讨论】:

    标签: javascript jestjs mocking aws-sdk aws-sdk-js


    【解决方案1】:

    您可以使用jest.spyOn(object, methodName) 模拟AWS.DynamoDB 类及其实例。

    使用mockFn.mockReturnThis()模拟方法链调用。

    例如

    dynamodb_content_access.js:

    const AWS = require('aws-sdk');
    
    class DynamoDBContentAccess {
      constructor() {
        this.dynamodb = new AWS.DynamoDB();
      }
      async findServices(bearerToken, filter, skip, limit) {
        const params = {
          TableName: 'Services',
        };
        const results = await this.dynamodb.batchGetItem(params).promise();
        return results;
      }
    }
    
    export default DynamoDBContentAccess;
    

    dynamodb_content_access.test.js:

    import DynamoDBContentAccess from './dynamodb_content_access';
    
    const AWS = require('aws-sdk');
    
    describe('services', () => {
      it('returns a list of services', async () => {
        const params = {
          bearerToken: 'fake bearer token',
          filter: {},
          skip: 10,
          limit: 10,
        };
        const mockResponse = {
          Responses: {
            Services: [
              {
                ServiceId: {
                  S: 'example1',
                },
              },
            ],
          },
        };
    
        const dynamodb = {
          batchGetItem: jest.fn().mockReturnThis(),
          promise: jest.fn().mockResolvedValueOnce(mockResponse),
        };
        const DynamoDBSpy = jest.spyOn(AWS, 'DynamoDB').mockImplementation(() => dynamodb);
        let serviceInterface = new DynamoDBContentAccess();
    
        const result = await serviceInterface.findServices(params);
        expect(result).toEqual(mockResponse);
        expect(DynamoDBSpy).toBeCalledTimes(1);
        expect(dynamodb.batchGetItem).toBeCalledWith({ TableName: 'Services' });
        expect(dynamodb.promise).toBeCalledTimes(1);
      });
    });
    

    单元测试结果:

     PASS  examples/66858621/dynamodb_content_access.test.js (7.959 s)
      services
        ✓ returns a list of services (3 ms)
    
    ----------------------------|---------|----------|---------|---------|-------------------
    File                        | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
    ----------------------------|---------|----------|---------|---------|-------------------
    All files                   |     100 |      100 |     100 |     100 |                   
     dynamodb_content_access.js |     100 |      100 |     100 |     100 |                   
    ----------------------------|---------|----------|---------|---------|-------------------
    Test Suites: 1 passed, 1 total
    Tests:       1 passed, 1 total
    Snapshots:   0 total
    Time:        8.846 s, estimated 10 s
    

    【讨论】:

    • 谢谢!这也适用于 AWS 的 es6 导入吗?
    猜你喜欢
    • 2021-02-26
    • 2020-02-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-24
    • 2021-03-23
    • 1970-01-01
    相关资源
    最近更新 更多