Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.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
Unit testing 开玩笑地跳过实现_Unit Testing_Jestjs - Fatal编程技术网

Unit testing 开玩笑地跳过实现

Unit testing 开玩笑地跳过实现,unit-testing,jestjs,Unit Testing,Jestjs,目前我有以下代码: function handleConnection(socket: Socket): void { info(`Connection started with socketId: ${socket.id}`) socket.on("joinRoom", (request: string) => handleJoinRoom(socket, request)); socket.on("shareData", (request: IShareDataRequ

目前我有以下代码:

function handleConnection(socket: Socket): void {
  info(`Connection started with socketId: ${socket.id}`)

  socket.on("joinRoom", (request: string) => handleJoinRoom(socket, request));

  socket.on("shareData", (request: IShareDataRequest) => handleShareData(socket, request));

  socket.on("disconnect", () => handleDisconnect(socket));
}
我想为joinRoom、shareData和disconnect等每个事件编写一个测试。要隔离测试,我只想测试第二个socket.onshareData,=>。。。调用并跳过第一个socket.onjoinRoom,=>。。。呼叫我尝试了多种mockImplementationOnce方法,但没有成功

我写的测试:

it('should emit data to room', () => {
    const listenMock = listen();
    const socketMock = {
      on: jest.fn()
        .mockImplementationOnce(() => null)
        .mockImplementationOnce((event: string, callback: Function) => callback({ roomId: "abcdefg" })),
      to: jest.fn().mockReturnValue({
        emit: jest.fn()
      })
    }
    jest.spyOn(listenMock, "on").mockImplementationOnce((event: string, callback: Function) => callback(socketMock))

    startSocket();

    expect(socketMock.to).toHaveBeenCalledWith("abcdefg");
    expect(socketMock.to().emit).toHaveBeenCalledWith("receiveData", expect.any(Object));
  })
共享数据功能:

function handleShareData(socket: Socket, request: IShareDataRequest): void {
  socket.to(request.roomId).emit("receiveData", request);
}

如果有人能帮我解决这个问题,我将不胜感激。

您可以尝试以下方法:

// define the mockSocket
const mockSocket = {
  // without any implementation
  on: jest.fn()
};

describe("connection handler", () => {
  // I personally like separating the test setup
  // in beforeAll blocks 
  beforeAll(() => {
    handleConnection(mockSocket);
  });

  // you can write assertions that .on
  // should have been called for each event
  // with a callback
  describe.each(["joinRoom", "shareData", "disconnect"])(
    "for event %s",
    event => {
      it("should attach joinRoom handlers", () => {
        expect(mockSocket.on.mock.calls).toEqual(
          expect.arrayContaining([[event, expect.any(Function)]])
        );
      });
    }
  );

  describe("joinRoom handler", () => {
    beforeAll(() => {
      // jest mock functions keep the calls internally
      // and you can use them to find the call with 
      // the event that you need and retrieve the callback
      const [_, joinRoomHandler] = mockSocket.on.mock.calls.find(
        ([eventName]) => eventName === "joinRoom"
      );

      // and then call it
      joinRoomHandler("mockRequestString");
    });

    it("should handle the event properly", () => {
      // your joinRoom handler assertions would go here
    });
  });
});