简体   繁体   English

在 controller 测试期间,Nestjs 注入的提供程序服务未定义

[英]Nestjs injected provider service is undefined during controller tests

The issue is a bit strange to me.这个问题对我来说有点奇怪。 I wrote a test for my Nest JS controller.我为我的 Nest JS controller 编写了一个测试。 The test looks like this:测试看起来像这样:

describe('Job Pipe Controller', () => {
    let controller: JobCustomController;
    let controller2: SetTagChoiceController;
    let moduleReference: TestingModule;

    beforeEach(async () => {
        moduleReference = await Test.createTestingModule({
            imports: [
                MongooseConnectionMockModule,
                AuthorizationManagerModule,
                UserManagerModule,
                TaskManagerModule,
                PaymentHistoryRepositoryModule,
                CustomManagerModule,
            ],
            providers: [],
            controllers: [JobCustomController, SetTagChoiceController],
        }).compile();

        controller = moduleReference.get<JobCustomController>(JobCustomController);
        controller2 = moduleReference.get<SetTagChoiceController>(SetTagChoiceController);
    });

    afterEach(async () => {
        await moduleReference.close();
    });

    it('Should do something to items', async () => {
        expect(controller.doItems()).toEqual({
            totalItemsCopied: 0,
            totalItemsToDistribute: 0,
        });
    });

    it('Should do something to items 2', async () => {
        expect(controller2.doItems()).toEqual({
            totalItemsCopied: 0,
            totalItemsToDistribute: 0,
        });
    });
  // ... other specs
});

As we can see, there are 2 tests, the first test (with controller) fails because sayMyName() is called from undefined :正如我们所见,有 2 个测试,第一个测试(使用控制器)失败,因为sayMyName()是从undefined调用的:

 ● Job Pipe Controller › Should do something to items

    TypeError: Cannot read property 'sayMyName' of undefined

      11 | 
      12 |  public doItems() {
    > 13 |      return this.jobCustom.sayMyName();
         |                            ^
      14 |  }
      15 | }
      16 | 

      at JobCustomController.doItems (api/src/app/jobs/controllers/job-custom.controller.ts:13:25)
      at Object.<anonymous> (api/src/app/jobs/controllers/job-pipe.controller.spec.ts:69:21)

The second one (with controller2) is passed.第二个(带有控制器2)通过。 I don't know why the injected jobCustom service is undefined in the JobCustomController while in the SetTagChoiceController, it's defined.我不知道为什么注入的jobCustom服务在 JobCustomController 中未定义,而在 SetTagChoiceController 中,它已定义。 I declare it the same way.我以同样的方式声明它。 To provide a context, I show the files below.为了提供上下文,我在下面展示了这些文件。

job-custom.service.ts工作-custom.service.ts

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

@Injectable()
export class JobCustom {
    public sayMyName() {
        return {
            totalItemsCopied: 0,
            totalItemsToDistribute: 0,
        };
    }
}

custom-manager.module.ts custom-manager.module.ts

import {Module} from '@nestjs/common';
import {JobRepositoryModule} from '@api/repositories/jobs';
import {JobCreationService} from './services/job-creation.service';
import {JobCustom} from './services/job-custom.service';

@Module({
    imports: [JobRepositoryModule],
    providers: [JobCreationService, JobCustom],
    exports: [JobCreationService, JobCustom],
})
export class CustomManagerModule {
    //
}

job-custom.controller.ts作业-custom.controller.ts

import {JobCustom} from '@api/providers/custom-manager';

export class JobCustomController {
    constructor(private readonly jobCustom: JobCustom) {
        //
    }

    public doItems() {
        return this.jobCustom.sayMyName();
    }
}

set-tag-choice.controller.ts设置标签选择.controller.ts

...other imports
import {JobCustom} from '@api/providers/custom-manager';

export class SetTagChoiceController {
    constructor(private readonly userTaskService: UserTaskService, private readonly jobCustom: JobCustom) {
        //
    }

    public doItems() {
        return this.jobCustom.sayMyName();
    }

  // ...other methods
}

Hope anyone can help me.希望任何人都可以帮助我。

If in testing: injection dependencies are undefined - this is some conflict of dependencies of dependencies.如果在测试中:注入依赖项未定义 - 这是依赖项的一些依赖项冲突。 Mock every dependency(provider) until it is fixed.模拟每个依赖项(提供者),直到它被修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM