Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 无法检查传递给Jest模拟对象的参数_Typescript_Unit Testing_Jestjs_Mocking - Fatal编程技术网

Typescript 无法检查传递给Jest模拟对象的参数

Typescript 无法检查传递给Jest模拟对象的参数,typescript,unit-testing,jestjs,mocking,Typescript,Unit Testing,Jestjs,Mocking,我有一个相当直接的Typescript类和方法。我想使用Jest测试此方法以模拟此方法的依赖关系: import {DynamoWorkflow} from '..'; import {Injectable} from '@nestjs/common'; @Injectable() export class PipelineService { getCoverageByServiceName(serviceName: string): Promise<QueryResult<

我有一个相当直接的Typescript类和方法。我想使用Jest测试此方法以模拟此方法的依赖关系:

import {DynamoWorkflow} from '..';
import {Injectable} from '@nestjs/common';

@Injectable()
export class PipelineService {
    getCoverageByServiceName(serviceName: string): Promise<QueryResult<PipelineEvent>> {
        return new DynamoWorkflow().queryPipelineEvents(serviceName);
    }
}
这段代码不会编译。消息是
类型“(serviceName:string)=>Promise.ts(2339)
上不存在属性“mock”

我犯了错误的代码块

我尝试了
expect(mockDynamo.queryPipelineEvents)但这也不会编译,错误是:
类型“MockedObjectDeep”上不存在属性“queryPipelineEvents”。ts(2339)

我还尝试了
expect(mockDynamo.mock.instances[0].queryPipelineEvents).toBeCalledWith(methodArg)。它编译和运行正常,但失败,并显示以下消息:

    expect(received).toBeCalledWith(...expected)

    Matcher error: received value must be a mock or spy function

    Received has value: undefined

      44 |     // expect(mockedDynamoMethod.mock.calls[0][0]).toHaveBeenCalledWith(methodArg);
      45 |     // expect(mockDynamo.queryPipelineEvents).toBeCalledWith(methodArg);
    > 46 |     expect(mockDynamo.mock.instances[0].queryPipelineEvents).toBeCalledWith(methodArg);
         |                                                              ^
      47 | 
      48 |     // Test #3: confirm the result is what we expect
      49 |     expect(actualResult).resolves.toEqual(mockQueryResult);


有人能帮助我理解如何确认模拟方法的参数与预期一致吗?

一位同事正在进行不同的测试,并意外地找到了答案。关键是将mock方法分配给被模拟的对象和方法。扔掉原始代码中的所有模拟,并将其替换为以下内容:

jest.mock('../../../../workflows/dynamoworkflow');
const mockMethod = jest.fn();
mockMethod.mockReturnValue(Promise.resolve(mockQueryResult));
DynamoWorkflow.prototype.queryPipelineEvents = mockMethod; //<- key here!
jest.mock('../../../../workflows/dynamoworkflow');
const mockMethod = jest.fn();
mockMethod.mockReturnValue(Promise.resolve(mockQueryResult));
DynamoWorkflow.prototype.queryPipelineEvents = mockMethod; //<- key here!
describe("PipelineService", () => {

  it("getCoverageByServiceName returns same data as db supplies", () => {
    //setup mock data...
    const mockPipelineEvent: PipelineEvent = new PipelineEvent();
    mockPipelineEvent.aut = "fake data";
    const mockPipelineEvents: PipelineEvent[] = [mockPipelineEvent];
    const mockQueryResult = {results: mockPipelineEvents, total: 1};

    //setup mock...
    jest.mock('../../../../workflows/dynamoworkflow');
    const mockMethod = jest.fn();
    mockMethod.mockReturnValue(Promise.resolve(mockQueryResult));
    DynamoWorkflow.prototype.queryPipelineEvents = mockMethod;

    //exercise the method we're testing...
    const methodArg = "foo-service";
    const serviceUnderTest = new PipelineService();
    const actualResult = serviceUnderTest.getCoverageByServiceName(methodArg);

    // and test...
    expect(mockMethod).toHaveBeenCalledWith(methodArg);
    expect(actualResult).resolves.toEqual(mockQueryResult);
  });

})