New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to unit test controller #1209
Comments
The simplest way is to mock DeviceService when you want to test DeviceController. // create mock class or you can use some package like sinon for this
class DeviceServiceMock extends BaseService {
async getDevices(group): Promise<any> {
return [];
}
}
describe('DeviceController', () => {
let deviceController: DeviceController;
let deviceService: DeviceService;
const response = {
send: (body?: any) => { },
status: (code: number) => response,
};
beforeEach(async () => {
const module = await Test.createTestingModule({
controllers: [DeviceController],
components: [{
provide: DeviceService,
useValue: new DeviceServiceMock() // or alternatively useClass: DeviceServiceMock
}],
}).compile();
deviceService = module.get<DeviceService>(DeviceService);
deviceController = module.get<DeviceController>(DeviceController);
});
describe('getDevices()', () => {
it('should return an array of devices', async () => {
const result = [{
Group: 'group_abc',
DeviceId: 'device_abc',
},
{
Group: 'group_xyz',
DeviceId: 'device_xyz',
}];
jest.spyOn(deviceService, 'getDevices').mockImplementation(() => result);
expect(await deviceController.getDevices(response, null)).toBe(result);
});
});
}); |
Small notes async getDevices(group): Promise<any> {
try {
return await this._deviceModel.find({ Group: group }).exec();
} catch (error) {
return Promise.reject(error);
}
} you shouldn't use
As for the your question, it's very hard to load all dependencies tree in the tests almost in all cases. Also when you load all dependencies, you need to be sure that all of them works without any errors. Because when your test fails, you need to know where something went wrong - in the tested class or in hes dependency. // there must be DeviceModelMock implementation
const deviceProvidersMock = [
{
provide: 'devices',
useValue: DeviceModelMock,
},
];
beforeEach(async () => {
const module = await Test.createTestingModule({
controllers: [DeviceController],
components: [DeviceService, ...deviceProvidersMock],
}).compile()
}); |
Thanks @andrew-yustyk for a huge explanation. @mukeshrawat02, additionally, I would not recommend using |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
I am getting issues while unit testing my controller. For maximum coverage I wanted to unit test controller and respective services and would like to mock external dependencies like mongoose connection etc.
I am getting error "Nest can't resolve dependencies of my service". I already tried suggestions mentioned by @adrien2p below, but didn't get any luck:
#194 (comment)
Please find my code below:
When I am running my code above, I am getting two errors:
The text was updated successfully, but these errors were encountered: