Nestjs 注入的提供程序服务在控制器测试期间未定义

时间:2021-04-22 03:44:42

标签: javascript node.js jestjs nestjs ts-jest

这个问题对我来说有点奇怪。我为我的 Nest JS 控制器编写了一个测试。测试如下:

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
});

如我们所见,有 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)

第二个(带控制器2)通过了。我不知道为什么注入的 jobCustom 服务在 JobCustomController 中未定义,而在 SetTagChoiceController 中已定义。我以同样的方式声明它。为了提供上下文,我在下面展示了文件。

job-custom.service.ts

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

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

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

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

...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
}

希望任何人都可以帮助我。

0 个答案:

没有答案