Skip to content

Instantly share code, notes, and snippets.

@AkshayCHD
Created January 12, 2022 04:30
Show Gist options
  • Save AkshayCHD/63502ca4a290a6a2a319e77e53f40ef3 to your computer and use it in GitHub Desktop.
Save AkshayCHD/63502ca4a290a6a2a319e77e53f40ef3 to your computer and use it in GitHub Desktop.
const { saveUser } = require("../src/services/user.service");
const { expect } = require("chai");
const mongoose = require("mongoose");
const username = "root";
const password = "example";
const dbname = "mongo-sinon";
mongoose.connect(
`mongodb://${username}:${password}@localhost:27017/${dbname}?authSource=admin&readPreference=primary`,
{
useNewUrlParser: true,
useUnifiedTopology: true,
}
);
const db = mongoose.connection;
db.on("error", console.error.bind(console, "connection error: "));
db.once("open", async function () {
console.log("Connected successfully");
});
describe("User Service Unit Tests", function () {
describe("Save User functionality", function () {
it("should successfully add a user if the number of users in the DB with the same profiled is zero", async function () {
const profileId = 1;
const name = "Akshay";
const dob = "2020-12-12";
const experience = [{ years: 2, organizationName: "ABCD" }];
const returnedUser = await saveUser({
profileId,
name,
dob,
experience,
});
expect(returnedUser.name).to.equal(name);
expect(returnedUser.dob.toString()).to.equal((new Date(dob)).toString());
experience.map((exp, index) => {
expect(returnedUser.experience[index].years).to.equal(exp.years);
expect(returnedUser.experience[index].organizationName).to.equal(exp.organizationName);
})
});
it("should throw an error if the number of users with the same profileId is not zero", async function () {});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment