Created
January 5, 2021 21:23
-
-
Save nishabe/48e3f143c82caccc5aa61323ab80c905 to your computer and use it in GitHub Desktop.
app.controller.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Test, TestingModule } from '@nestjs/testing'; | |
import { AppController } from './app.controller'; | |
import { AppService } from './app.service'; | |
import { StudentService } from './student/student.service'; | |
describe('AppController', () => { | |
let appController: AppController; | |
let spyService: StudentService; | |
beforeEach(async () => { | |
const ApiServiceProvider = { | |
provide: StudentService, | |
useFactory: () => ({ | |
getGpa: jest.fn(() => 4.5), | |
}), | |
}; | |
const app: TestingModule = await Test.createTestingModule({ | |
controllers: [AppController], | |
providers: [AppService, ApiServiceProvider], | |
}).compile(); | |
appController = app.get<AppController>(AppController); | |
spyService = app.get<StudentService>(StudentService); | |
}); | |
describe('root', () => { | |
it('should return "Hello World!"', () => { | |
expect(appController.getHello()).toBe('Hello World!'); | |
}); | |
}); | |
describe('getGPA', () => { | |
it('should call getGPA for a student', async () => { | |
const firstName = 'Joe'; | |
const secondName = 'Foo'; | |
appController.getStudentGpa(firstName, secondName); | |
expect(spyService.getGpa).toHaveBeenCalled(); | |
}); | |
}); | |
describe('getGPA', () => { | |
it('should retrieve getGPA for a student', async () => { | |
const firstName = 'Joe'; | |
const secondName = 'Foo'; | |
expect(spyService.getGpa(firstName, secondName)).toBe(4.5); | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment