In the same manner that we refactor the objects,we have now to refactor the service that is called by the WebAPI. Yes,we have the skinny controllers concept – but the service is something new for the application. Think about retrieving the list of the meetings – this is something that we did not need in the console application( it was just the current meeting)
So how to test ? The easy way is to put the service ( with the dependencies) in a new project – bingo-cards-api-objects. Then reference the project from bingo-cards-api nestjs webAPI – and ensure it works. Now it is the time for tests:
describe("Meetings API service creation",() => {
it("a meeting should have been created properly ",async () => {
const ms: MeetingService =new MeetingService();
const userName:string = "Andrei";
const meetingName:string = "Meeting today"
const m:Meeting =await ms.create(userName,meetingName);
expect(m.Name).toBe(meetingName);
expect(m.Participants.length).toBe(1);
expect(m.Participants[0].Name).toBe(userName);
});
it("a meeting should have been retrieved properly ",async () => {
const ms: MeetingService =new MeetingService();
const userName:string = "Andrei";
const meetingName:string = "Meeting today"
const m :Meeting=await ms.create(userName,meetingName);
const retr :ActualMeeting[]= ms.ActualMeetings();
expect(retr.length).toBe(1);
expect(retr[0].idMeeting).toBe(m.Id);
expect(retr[0].participantName).toBe(userName);
});
});
And that is not all. As the code for checkCard look,it should be more tests….
public checkCard(idMeeting: any,idCard: number,nameParticipant: string ): Meeting{
const m = this.meetings.find(it => it.Id === idMeeting );
// TODO: throw if meeting is null
const c = m.FindCard(idCard);
// TODO: throw if card is null
const p = m.FindParticipantAfterName(nameParticipant);
// TODO: throw if participant is null
m.CheckCardByParticipant(c,p);
return m;
}
Not that I have time for this…
Leave a Reply