Hey zusammen,
ich beschäftige mich gerade mit Unit Tests und benutze unter node.js dafür die Bibliothek Mocha.js.
Ich schreibe da gerade ein paar Unit Tests das erste Mal und stelle mir gerade die Frage, wie ich die update-Funktion des Webservices testen kann. Ich habe schon einiges getestet von assert.ok() bis hin zu assert.equals(x, y) aber die Console zeigt mir da immer ein falsches Testergebnis an.
Habt ihr eine Idee wie ich die Update Funktion testen kann?
Desweiteren stelle ich mir die Frage ob die Unit Tests so korrekt sind. Es wäre echt toll, wenn ihr euch diese anschaut und mir da Feedback gibt. Darüber wäre ich sehr dankbar und würde mich auch sehr freuen.
Anbei findet ihr noch ein Bild des Testdurchlaufs in der Console.
Hier ist der Code:
- module.exports = class ContactManager{
- constructor(){
- this.contacts = new Map();
- // simulate Testcontact
- let contact = {
- firstName: "Stef",
- lastName: "test",
- email: "test@example.de"
- };
- this.contacts.set(1, contact);
- this.contactId = 1;
- }
- async addContact(contact){
- this.contactId++;
- contact.id = this.contactId;
- // Schlüssel (this.contactId) => Wert (contact) Paare
- this.contacts.set(this.contactId, contact);
- return this.contactId;
- }
- async getContact(id){
- return this.contacts.get(id);
- }
- // Hier ist die Update-Funktion
- async updateContact(id, contact){
- if(this.contacts.set(id, contact)){
- return true;
- }else{
- return false;
- }
- }
- async deleteContact(id){
- this.contacts.delete(id);
- }
- async getContacts(){
- return Array.from(this.contacts.values());
- }
- }
- let ContactManager = require("./classes/ContactManager");
- let assert = require('assert');
- describe('addContact()', function () {
- it('should add a new contact in our map', async function () {
- // Setup Phase
- let contactsManager = new ContactManager();
- let newContact = {
- firstName: "Test",
- lastName: "Mustermann",
- email: "test@example.de",
- };
- // Execute Phase
- await contactsManager.addContact(newContact);
- // Verify Phase
- let numberOfContacts = contactsManager.contacts.size;
- assert.equal(numberOfContacts, 1);
- });
- });
- describe('getContact()', function (){
- it('should return a contact by Id', async function (){
- let contactsManager = new ContactManager();
- let id = 1;
- let contact = await contactsManager.getContact(id);
- assert.ok(contact);
- });
- });
- describe('getContacts()', function (){
- it('should return all contacts', async function() {
- let contactsManager = new ContactManager();
- let contacts = await contactsManager.getContacts();
- assert.ok(contacts);
- });
- });
- // Hier ist der UnitTest für die Updatefunktion
- describe('updateContact()', function (){
- it('should update a contact by id', async function(){
- // Setup Phase
- let contactsManager = new ContactManager();
- let id = 2;
- let contact = {
- firstName: "Test",
- lastName: "Mustermann",
- email: "test@example.de",
- };
- // Execute Phase
- let update = await contactsManager.updateContact(id, contact);
- // Verify Phase
- assert.equals(update, true);
- });
- });
- describe('deleteContact()', function (){
- it('should delete a contact by id', async function (){
- let contactsManager = new ContactManager();
- let id = 1;
- await contactsManager.deleteContact(id);
- let numberOfContacts = contactsManager.contacts.size;
- assert.equal(numberOfContacts, 0);
- });
- });
Schöne Grüße,
Stef