| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- import { Test, TestingModule } from '@nestjs/testing'
- import { AppController } from './app.controller'
- import { AppService } from './app.service'
- class MockResponse {
- res: any
- constructor() {
- this.res = {}
- }
- status = jest
- .fn()
- .mockReturnThis()
- .mockImplementationOnce((code) => {
- this.res.code = code
- return this
- })
- send = jest
- .fn()
- .mockReturnThis()
- .mockImplementationOnce((message) => {
- this.res.message = message
- return this
- })
- json = jest
- .fn()
- .mockReturnThis()
- .mockImplementationOnce((json) => {
- this.res.json = json
- return this
- })
- }
- describe('AppController', () => {
- let appController: AppController
- let appService: AppService
- const response = new MockResponse()
- beforeEach(async () => {
- const app: TestingModule = await Test.createTestingModule({
- controllers: [AppController],
- providers: [
- {
- provide: AppService,
- useValue: {
- getHello: jest.fn(() => {}),
- getSecureResource: jest.fn(() => {})
- }
- }
- ]
- }).compile()
- appController = app.get<AppController>(AppController)
- appService = app.get<AppService>(AppService)
- })
- describe('root', () => {
- it('should be defined', () => {
- expect(appController).toBeDefined()
- })
- it('should call method getHello() in AppService', () => {
- const createSpy = jest.spyOn(appService, 'getHello')
- appController.getHello(response as any)
- expect(createSpy).toHaveBeenCalled()
- })
- it('should call method getProtectedResource() in AppService', () => {
- const createSpy = jest.spyOn(appService, 'getSecureResource')
- appController.getProtectedResource(response as any)
- expect(createSpy).toHaveBeenCalled()
- })
- })
- })
|