Skip to content

Commit e113855

Browse files
Fix error handling in IdentitiesController
1 parent e936de7 commit e113855

File tree

1 file changed

+30
-25
lines changed

1 file changed

+30
-25
lines changed

src/management/identities/identities.controller.spec.ts

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,12 @@ describe('IdentitiesController', () => {
7878
});
7979

8080
it('should throw an error when creating an identity', async () => {
81-
jest.spyOn(service, 'create').mockImplementationOnce(() => {
82-
throw new Error('Error');
83-
});
84-
const createIdentity = await controller.create(response, IdentitiesDtoStub());
85-
expect(createIdentity.statusCode).toBe(HttpStatus.BAD_REQUEST);
81+
jest.spyOn(service, 'create').mockRejectedValueOnce(new Error('Error'));
82+
try {
83+
await controller.create(response, IdentitiesDtoStub());
84+
} catch (error) {
85+
expect(response.statusCode).toBe(HttpStatus.BAD_REQUEST);
86+
}
8687
});
8788
});
8889

@@ -93,11 +94,12 @@ describe('IdentitiesController', () => {
9394
});
9495

9596
it('should throw an error when searching identities', async () => {
96-
jest.spyOn(service, 'findAndCount').mockImplementationOnce(() => {
97-
throw new Error('Error');
98-
});
99-
const searchIdentity = await controller.search(response, {}, searchFilterOptions);
100-
expect(searchIdentity.statusCode).toBe(HttpStatus.BAD_REQUEST);
97+
jest.spyOn(service, 'findAndCount').mockRejectedValueOnce(new Error('Error'));
98+
try {
99+
await controller.search(response, {}, searchFilterOptions);
100+
} catch (error) {
101+
expect(response.statusCode).toBe(HttpStatus.BAD_REQUEST);
102+
}
101103
});
102104
});
103105

@@ -110,11 +112,12 @@ describe('IdentitiesController', () => {
110112
});
111113

112114
it('should throw an error when finding an identity', async () => {
113-
jest.spyOn(service, 'findOne').mockImplementationOnce(() => {
114-
throw new Error('Error');
115-
});
116-
const findIdentity = await controller.read(_id, response);
117-
expect(findIdentity.statusCode).toBe(HttpStatus.BAD_REQUEST);
115+
jest.spyOn(service, 'findOne').mockRejectedValueOnce(new Error('Error'));
116+
try {
117+
await controller.read(_id, response);
118+
} catch (error) {
119+
expect(response.statusCode).toBe(HttpStatus.BAD_REQUEST);
120+
}
118121
});
119122
});
120123

@@ -127,11 +130,12 @@ describe('IdentitiesController', () => {
127130
});
128131

129132
it('should throw an error when updating an identity', async () => {
130-
jest.spyOn(service, 'update').mockImplementationOnce(() => {
131-
throw new Error('Error');
132-
});
133-
const updateIdentity = await controller.update(_id, IdentitiesDtoStub(), response);
134-
expect(updateIdentity.statusCode).toBe(HttpStatus.BAD_REQUEST);
133+
jest.spyOn(service, 'update').mockRejectedValueOnce(new Error('Error'));
134+
try {
135+
await controller.update(_id, IdentitiesDtoStub(), response);
136+
} catch (error) {
137+
expect(response.statusCode).toBe(HttpStatus.BAD_REQUEST);
138+
}
135139
});
136140
});
137141

@@ -144,11 +148,12 @@ describe('IdentitiesController', () => {
144148
});
145149

146150
it('should throw an error when deleting an identity', async () => {
147-
jest.spyOn(service, 'delete').mockImplementationOnce(() => {
148-
throw new Error('Error');
149-
});
150-
const deleteIdentity = await controller.remove(_id, response);
151-
expect(deleteIdentity.statusCode).toBe(HttpStatus.BAD_REQUEST);
151+
jest.spyOn(service, 'delete').mockRejectedValueOnce(new Error('Error'));
152+
try {
153+
await controller.remove(_id, response);
154+
} catch (error) {
155+
expect(response.statusCode).toBe(HttpStatus.BAD_REQUEST);
156+
}
152157
});
153158
});
154159
});

0 commit comments

Comments
 (0)