Se2 Exp7
Se2 Exp7
Theory:
Unit testing is a software testing technique where individual units or
Unit testing involves writing test cases for each unit of code to verify its
behavior against expected outcomes. These test cases are usually small
and focused, targeting specific functionalities or scenarios. By testing
units in isolation, developers can easily pinpoint and fix any bugs or
issues, without needing to navigate through the entire codebase.
Mocha and Chai are popular JavaScript testing frameworks used for
writing and running unit tests. Mocha provides a flexible and feature-
rich test runner that supports various testing styles (e.g., BDD, TDD) and
asynchronous testing. Chai, on the other hand, is an assertion library that
works seamlessly with Mocha and provides expressive and readable
assertions, making test cases easier to write and understand.
NYC (or Istanbul) is a code coverage tool for JavaScript that works well
beforeEach(() => {
// Create stubs for all Mongoose methods used in the controller
findStub = sinon.stub(Cardmodel, "find");
findByIdStub = sinon.stub(Cardmodel, "findById");
createStub = sinon.stub(Cardmodel, "create");
findOneAndUpdateStub = sinon.stub(Cardmodel, "findOneAndUpdate");
findOneAndDeleteStub = sinon.stub(Cardmodel, "findOneAndDelete");
afterEach(() => {
// Restore all stubs
sinon.restore();
});
expect(res.status).to.equal(200);
expect(res.body).to.be.an("array");
expect(res.body.length).to.be.at.most(12); // Check limit is
working
expect(findStub.calledOnce).to.be.true;
});
it("should handle database errors when listing properties", async
() => {
// Override the stub to throw an error
findStub.restore();
findStub = sinon
.stub(Cardmodel, "find")
.throws(new Error("Database connection error"));
expect(res.status).to.equal(500);
expect(res.body).to.have.property("error");
});
});
expect(res.status).to.equal(200);
expect(res.body).to.be.an("object");
expect(res.body).to.have.property("name", sampleCard.name);
});
expect(res.status).to.equal(404);
expect(res.body).to.have.property("error", "No Such Card
Exists");
});
expect(res.status).to.equal(404);
expect(res.body).to.have.property("error", "No such Cards
exists");
});
describe("PATCH /:id - Update a property", () => {
it("should update a property with valid ID and data", async () => {
const updateData = {
price: "1600000",
description: "Updated description for test property",
};
const updatedProperty = {
...sampleCard,
...updateData,
};
findOneAndUpdateStub.resolves(updatedProperty);
expect(res.status).to.equal(200);
expect(res.body).to.be.an("object");
expect(res.body).to.have.property("price", updateData.price);
expect(res.body).to.have.property("description",
updateData.description);
});
expect(res.status).to.equal(404);
expect(res.body).to.have.property("error", "No Such Card
Exists");
});
expect(res.status).to.equal(404);
expect(res.body).to.have.property("error", "No such Cards
exists");
});
expect(res.status).to.equal(500);
expect(res.body).to.have.property("error");
});
});
expect(res.status).to.equal(200);
expect(res.body).to.be.an("object");
expect(res.body).to.have.property("_id", sampleCard._id);
});
expect(res.status).to.equal(404);
expect(res.body).to.have.property("error", "No Such Card
Exists");
});
expect(res.status).to.equal(404);
expect(res.body).to.have.property("error", "No such Cards
exists");
});
expect(res.status).to.equal(500);
expect(res.body).to.have.property("error");
});
});