Skip to content
Snippets Groups Projects
Commit 4c29a534 authored by Pedro Pablo Cardona Arroyave's avatar Pedro Pablo Cardona Arroyave
Browse files

Merge branch '51-make-a-test-guide-for-backend' into 'main'

A guide for testing was added

Closes #51

See merge request !18
parents f5a4b073 d0db1fd9
No related branches found
No related tags found
1 merge request!18A guide for testing was added
Pipeline #235559 passed with stages
in 46 seconds
This diff is collapsed.
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
"description": "", "description": "",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"test": "echo \"Error: no test specified\" && exit 1", "test": "mocha",
"start": "nodemon index.js", "start": "nodemon index.js",
"format": "prettier --write ." "format": "prettier --write ."
}, },
...@@ -23,5 +23,10 @@ ...@@ -23,5 +23,10 @@
"swagger-jsdoc": "^6.2.8", "swagger-jsdoc": "^6.2.8",
"swagger-ui-express": "^5.0.0", "swagger-ui-express": "^5.0.0",
"winston": "^3.10.0" "winston": "^3.10.0"
},
"devDependencies": {
"chai": "^4.3.10",
"mocha": "^10.2.0",
"sinon": "^16.1.0"
} }
} }
const { expect } = require("chai");
const { MongoClient } = require("mongodb"); // Import MongoClient for test setup
const { addQuestion, getQuestion } = require("../controllers/question.controller");
const { connectToDatabase } = require("../utils/mongoDB");
describe("Question Controller Tests", () => {
let client;
before(async () => {
client = await connectToDatabase();
});
after(async () => {
await client.close();
});
beforeEach(async () => {
// Clear the questions collection before each test
const db = client.db("TrioTech");
await db.collection("questions").deleteMany({});
});
it("should add a question", async () => {
const questionData = {
text: "What is the capital of France?",
category: 123, // You should use a valid category ID here
};
const result = await addQuestion(questionData);
expect(result.insertedCount).to.equal(1);
expect(result.ops[0].text).to.equal(questionData.text);
});
it("should get a question by ID", async () => {
// Insert a sample question into the database for testing
const sampleQuestion = {
text: "Sample Question",
category: "123", // You should use a valid category ID here
};
const db = client.db("TrioTech");
const questionCollection = db.collection("questions");
const insertedQuestion = await questionCollection.insertOne(sampleQuestion);
const questionId = insertedQuestion.insertedId.toString();
const result = await getQuestion(questionId);
expect(result).to.not.be.null;
expect(result.text).to.equal(sampleQuestion.text);
});
});
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment