TypeError:XXXXXXX不是函数(在开玩笑的同时)

飞鸟

我正在努力嘲笑下面的方法。无法嘲笑。请在下面查看我的测试用例。测试用例因错误而失败

TypeError:XXXXXXX不是函数。

当我运行覆盖率报告时,它说所有行都被覆盖。我想念的是什么?

import { Lambda } from "aws-sdk";
import fsReadFilePromise = require("fs-readfile-promise");

export class AWSLambdaDeployer {

    readonly mylambda: Lambda;

     public async deploy(zipPath: string, fName: string) {

        const fileData = await fsReadFilePromise(zipPath);

        const params: Lambda.Types.UpdateFunctionCodeRequest = {
            FunctionName: fName,
            ZipFile: fileData,
            Publish: true
        };

        return this.mylambda.updateFunctionCode(params).promise();
    }
}

下面是我开玩笑的测试用例


const mockUpdateFunctionCode = jest.fn().mockResolvedValueOnce('Ready');

jest.mock("../node_modules/aws-sdk/clients/lambda", () =>{
    return{
        updateFunctionCode: mockUpdateFunctionCode,     
    }
});

jest.mock("fs-readfile-promise", () => {
    return jest.fn();
});

import { Lambda } from "aws-sdk";
import fsReadFilePromise = require("fs-readfile-promise");
import { AWSLambdaDeployer } from "../src/index";

describe('LambdaDeployer class', () => {

    afterEach(() => {
     jest.resetAllMocks();
     jest.restoreAllMocks();
    });
           it(' functionality under test', async () =>{

             (fsReadFilePromise as any).mockResolvedValueOnce('get it done');

             const awslambdaDeployer = new AWSLambdaDeployer();

            const actual = await awslambdaDeployer.deploy('filePath', 'workingFunction');

             expect(fsReadFilePromise).toBeCalledWith('filePath');      
           })
});

这给我错误如下。测试用例失败。覆盖率报告显示了覆盖的所有行。

TypeError:this.mylambda.updateFunctionCode不是函数

幻灯片放映

这是单元测试解决方案:

index.ts

import { Lambda } from 'aws-sdk';
import fsReadFilePromise from 'fs-readfile-promise';

export class AWSLambdaDeployer {
  private readonly mylambda: Lambda;
  constructor(lambda: Lambda) {
    this.mylambda = lambda;
  }

  public async deploy(zipPath: string, fName: string) {
    const fileData = await fsReadFilePromise(zipPath);

    const params: Lambda.Types.UpdateFunctionCodeRequest = {
      FunctionName: fName,
      ZipFile: fileData,
      Publish: true,
    };

    return this.mylambda.updateFunctionCode(params).promise();
  }
}

index.spec.ts

import { AWSLambdaDeployer } from './';
import fsReadFilePromise from 'fs-readfile-promise';
import AWS from 'aws-sdk';

jest.mock('aws-sdk', () => {
  const mLambda = {
    updateFunctionCode: jest.fn().mockReturnThis(),
    promise: jest.fn(),
  };
  return {
    Lambda: jest.fn(() => mLambda),
  };
});

jest.mock('fs-readfile-promise', () => jest.fn());

describe('59463491', () => {
  afterEach(() => {
    jest.resetAllMocks();
    jest.restoreAllMocks();
  });
  it(' functionality under test', async () => {
    (fsReadFilePromise as any).mockResolvedValueOnce('get it done');

    const lambda = new AWS.Lambda();
    const awslambdaDeployer = new AWSLambdaDeployer(lambda);

    await awslambdaDeployer.deploy('filePath', 'workingFunction');

    expect(fsReadFilePromise).toBeCalledWith('filePath');
    expect(lambda.updateFunctionCode).toBeCalledWith({
      FunctionName: 'workingFunction',
      ZipFile: 'get it done',
      Publish: true,
    });
    expect(lambda.updateFunctionCode().promise).toBeCalledTimes(1);
  });
});

单元测试结果覆盖率100%:

 PASS  src/stackoverflow/59463491/index.spec.ts (10.592s)
  59463491
    ✓  functionality under test (9ms)

----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |      100 |      100 |      100 |      100 |                   |
 index.ts |      100 |      100 |      100 |      100 |                   |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        12.578s, estimated 13s

源代码:https : //github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/59463491

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

开玩笑的React示例

来自分类Dev

与coffeescript jsx开玩笑吗?

来自分类Dev

用getSourceAsObjectList()开玩笑

来自分类Dev

开玩笑抛出TypeError:this.inputEl.focus不是函数#1964

来自分类Dev

开玩笑-断言异步函数引发测试失败

来自分类Dev

开玩笑嘲笑特定功能

来自分类Dev

开玩笑-react-automata-未定义不是对象(评估“ machine.states”)

来自分类Dev

开玩笑地模拟一个toPromise函数.toPromise不是一个函数

来自分类Dev

开玩笑抱怨错误...我告诉开玩笑期望

来自分类Dev

开玩笑SpyOn值而不是getter

来自分类Dev

开玩笑-模拟update()函数

来自分类Dev

开玩笑-ReferenceError:导入的函数未定义

来自分类Dev

为什么开玩笑不调用useEffect挂钩内的函数?

来自分类Dev

开玩笑TypeError:Document.save不是函数

来自分类Dev

在函数内部开玩笑

来自分类Dev

开玩笑的自动模拟失败:TypeError:fs.readdirSync不是一个函数

来自分类Dev

开玩笑-函数fs.writefile中的模拟回调

来自分类Dev

开玩笑:模拟构造函数

来自分类Dev

从Promise开玩笑的模拟回调函数

来自分类Dev

开玩笑-dontMock函数无法按预期工作

来自分类Dev

开玩笑TypeError:configService.get不是一个函数

来自分类Dev

开玩笑/反应/ Mobx:TypeError mobxReact.observer不是函数

来自分类Dev

开玩笑:模拟构造函数以返回错误

来自分类Dev

开玩笑如何测试调用函数的行?

来自分类Dev

开玩笑:TypeError:无法读取未定义的属性'foo'

来自分类Dev

开玩笑的测试-TypeError:无法读取null的属性“ filter”

来自分类Dev

开玩笑未能使用泛型编译函数

来自分类Dev

无法模拟createQueryBuilder函数typeorm nestjs开玩笑

来自分类Dev

开玩笑instace()。simulate('dayClick'); TypeError不存在

Related 相关文章

  1. 1

    开玩笑的React示例

  2. 2

    与coffeescript jsx开玩笑吗?

  3. 3

    用getSourceAsObjectList()开玩笑

  4. 4

    开玩笑抛出TypeError:this.inputEl.focus不是函数#1964

  5. 5

    开玩笑-断言异步函数引发测试失败

  6. 6

    开玩笑嘲笑特定功能

  7. 7

    开玩笑-react-automata-未定义不是对象(评估“ machine.states”)

  8. 8

    开玩笑地模拟一个toPromise函数.toPromise不是一个函数

  9. 9

    开玩笑抱怨错误...我告诉开玩笑期望

  10. 10

    开玩笑SpyOn值而不是getter

  11. 11

    开玩笑-模拟update()函数

  12. 12

    开玩笑-ReferenceError:导入的函数未定义

  13. 13

    为什么开玩笑不调用useEffect挂钩内的函数?

  14. 14

    开玩笑TypeError:Document.save不是函数

  15. 15

    在函数内部开玩笑

  16. 16

    开玩笑的自动模拟失败:TypeError:fs.readdirSync不是一个函数

  17. 17

    开玩笑-函数fs.writefile中的模拟回调

  18. 18

    开玩笑:模拟构造函数

  19. 19

    从Promise开玩笑的模拟回调函数

  20. 20

    开玩笑-dontMock函数无法按预期工作

  21. 21

    开玩笑TypeError:configService.get不是一个函数

  22. 22

    开玩笑/反应/ Mobx:TypeError mobxReact.observer不是函数

  23. 23

    开玩笑:模拟构造函数以返回错误

  24. 24

    开玩笑如何测试调用函数的行?

  25. 25

    开玩笑:TypeError:无法读取未定义的属性'foo'

  26. 26

    开玩笑的测试-TypeError:无法读取null的属性“ filter”

  27. 27

    开玩笑未能使用泛型编译函数

  28. 28

    无法模拟createQueryBuilder函数typeorm nestjs开玩笑

  29. 29

    开玩笑instace()。simulate('dayClick'); TypeError不存在

热门标签

归档