笑话 - 如何模拟其构造函数接受参数的服务

发布于 2025-01-10 07:27:57 字数 890 浏览 1 评论 0原文

启动 NodeJS 应用程序时我有这样的逻辑:

  const twiddleService = new twiddleService(
    new twiddleClient(),
    new UserRepository(),
    new BusinessRepository()
  );
  const twiddleController = new twiddleController(twiddleService);

我通过模拟 twiddleClient 对 twiddleService 进行了单元测试。

现在我想对 twiddleController 和模拟 twiddleService 进行单元测试。

在我的 twiddleController.test.ts 文件中,我

import { twiddleService } from '../../src/services/twiddle-service';
jest.mock('../../src/services/twiddle-service');
const twiddleController = new twiddleController(new twiddleService());

显然这不起作用,因为 twiddleService 需要 3 个参数。我可以再次模拟 twiddleClient 和存储库,但理想情况下我不会。

基本上,我的目标是我希望能够做类似的事情

jest.spyOn(TwiddleService, 'createBananas').mockResolvedValue('b');

,以便我可以对我的控制器进行单元测试。

解决这个问题的最佳实践是什么?

[我也在使用打字稿]

I have this logic when starting NodeJS app:

  const twiddleService = new twiddleService(
    new twiddleClient(),
    new UserRepository(),
    new BusinessRepository()
  );
  const twiddleController = new twiddleController(twiddleService);

I've unit tested twiddleService by mocking twiddleClient.

Now I want to unit test twiddleController and mock twiddleService.

In my twiddleController.test.ts file I have

import { twiddleService } from '../../src/services/twiddle-service';
jest.mock('../../src/services/twiddle-service');
const twiddleController = new twiddleController(new twiddleService());

Obviously this doesn't work because twiddleService expects 3 arguments. I could mock twiddleClient and the repositories again, but ideally I wouldn't.

Basically the goal is I want to be able to do something like

jest.spyOn(TwiddleService, 'createBananas').mockResolvedValue('b');

So that I can unit test my controller.

What are best practices when it comes to solving this problem?

[Also I'm using typescript]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小ぇ时光︴ 2025-01-17 07:27:57

我认为您根本不需要在 twiddle-service 上导入和调用 jest.mock

由于您使用依赖注入twiddleController提供twiddleService实例code> 构造函数,您的测试可以向 new twiddleController() 调用提供一个简单的对象(当然,符合 Twiddle Service 的接口)。您可以使用 jest.fnimplementation 参数来定义服务的 createBananas 方法返回给 twiddleController 的内容> 实例。结果测试如下所示:

describe("TwiddleController", () => {
  test("calls createBananas method of twiddleService instance", () => {
    const mockTwiddleService = {
      createBananas: jest.fn(() => "b"),
    };

    const twiddleController = new TwiddleController(mockTwiddleService);

    expect(twiddleController.doSomethingWithBananas()).toBe("b");
  });
});

I don't think you need to import and call jest.mock on the twiddle-service at all.

Since you are using Dependency Injection to provide the twiddleService instance to the twiddleController constructor, your test can supply a simple object - conforming to the Twiddle Service's interface, of course - to the new twiddleController() call. You can use jest.fn with an implementation argument to define what gets returned by the service's createBananas method to the twiddleController instance. The resulting test would look something like the following:

describe("TwiddleController", () => {
  test("calls createBananas method of twiddleService instance", () => {
    const mockTwiddleService = {
      createBananas: jest.fn(() => "b"),
    };

    const twiddleController = new TwiddleController(mockTwiddleService);

    expect(twiddleController.doSomethingWithBananas()).toBe("b");
  });
});

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文