Skip to content

Instantly share code, notes, and snippets.

@nishabe
Created January 5, 2021 21:23
Show Gist options
  • Save nishabe/48e3f143c82caccc5aa61323ab80c905 to your computer and use it in GitHub Desktop.
Save nishabe/48e3f143c82caccc5aa61323ab80c905 to your computer and use it in GitHub Desktop.
app.controller.spec.ts
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